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

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 (141) hide show
  1. package/LICENSE +14 -3
  2. package/README.md +497 -0
  3. package/dist/converter.min.cjs +210 -282
  4. package/dist/i3s-server/bin/i3s-server.min.cjs +106 -341
  5. package/dist/index.cjs +38586 -0
  6. package/dist/index.js +38557 -0
  7. package/dist/v5/adapters.cjs +3548 -0
  8. package/dist/v5/adapters.d.ts +33 -0
  9. package/dist/v5/adapters.js +3537 -0
  10. package/dist/v5/browser-sink.d.ts +37 -0
  11. package/dist/v5/browser-source.d.ts +38 -0
  12. package/dist/v5/browser-tileset-source.d.ts +70 -0
  13. package/dist/v5/browser.cjs +76 -0
  14. package/dist/v5/browser.d.ts +44 -0
  15. package/dist/v5/browser.js +75 -0
  16. package/dist/v5/conversion-api.d.ts +122 -0
  17. package/dist/v5/conversion-archive.d.ts +32 -0
  18. package/dist/v5/core.cjs +917 -0
  19. package/dist/v5/core.d.ts +16 -0
  20. package/dist/v5/core.js +899 -0
  21. package/dist/v5/feature-arrow.d.ts +36 -0
  22. package/dist/v5/i3s-mesh-conversion.d.ts +32 -0
  23. package/dist/v5/i3s-mesh-sink.d.ts +25 -0
  24. package/dist/v5/i3s-mesh-source-features.d.ts +13 -0
  25. package/dist/v5/i3s-mesh-source-material.d.ts +5 -0
  26. package/dist/v5/i3s-mesh-source.d.ts +30 -0
  27. package/dist/v5/index.cjs +73 -0
  28. package/dist/v5/index.d.ts +40 -0
  29. package/dist/v5/index.js +75 -0
  30. package/dist/v5/mesh-conversion.d.ts +71 -0
  31. package/dist/v5/mesh-draco.d.ts +8 -0
  32. package/dist/v5/mesh-features.d.ts +9 -0
  33. package/dist/v5/mesh-source-features.d.ts +21 -0
  34. package/dist/v5/mesh-source-texture.d.ts +6 -0
  35. package/dist/v5/mesh-source.d.ts +41 -0
  36. package/dist/v5/mesh-tileset-archive.d.ts +18 -0
  37. package/dist/v5/mesh-tileset-sink.d.ts +51 -0
  38. package/dist/v5/mesh.d.ts +86 -0
  39. package/dist/v5/point-cloud-conversion.d.ts +31 -0
  40. package/dist/v5/point-cloud-source-encoder.d.ts +46 -0
  41. package/dist/v5/point-cloud-source.d.ts +25 -0
  42. package/dist/v5/point-cloud-spatial.d.ts +22 -0
  43. package/dist/v5/point-cloud-tileset-sink.d.ts +28 -0
  44. package/dist/v5/point-cloud.d.ts +49 -0
  45. package/dist/v5/resource-fetcher.d.ts +18 -0
  46. package/dist/v5/resource-manifest.d.ts +58 -0
  47. package/dist/v5/spatial-conversion.d.ts +61 -0
  48. package/dist/v5/tileset-source.d.ts +21 -0
  49. package/package.json +52 -26
  50. package/src/common/README.md +5 -0
  51. package/src/index.ts +1 -2
  52. package/src/{3d-tiles-converter → v4/3d-tiles-converter}/3d-tiles-converter.ts +13 -2
  53. package/src/v4/3d-tiles-converter/helpers/validate-i3s-spatial-reference.ts +124 -0
  54. package/src/{deps-installer → v4/deps-installer}/deps-installer.ts +10 -7
  55. package/src/{i3s-converter → v4/i3s-converter}/helpers/batch-ids-extensions.ts +1 -1
  56. package/src/v4/i3s-converter/helpers/encode-string-attribute.ts +23 -0
  57. package/src/{i3s-converter → v4/i3s-converter}/helpers/geometry-converter.ts +30 -55
  58. package/src/{i3s-converter → v4/i3s-converter}/helpers/gltf-attributes.ts +5 -4
  59. package/src/{i3s-converter → v4/i3s-converter}/helpers/load-3d-tiles.ts +5 -9
  60. package/src/{i3s-converter → v4/i3s-converter}/i3s-converter.ts +6 -7
  61. package/src/{i3s-server → v4/i3s-server}/README.md +2 -2
  62. package/src/{i3s-server → v4/i3s-server}/routes/slpk-router.ts +1 -0
  63. package/src/v4/index.ts +2 -0
  64. package/src/{lib → v4/lib}/utils/compress-util.ts +8 -7
  65. package/src/v5/adapters.ts +75 -0
  66. package/src/v5/browser-sink.ts +130 -0
  67. package/src/v5/browser-source.ts +155 -0
  68. package/src/v5/browser-tileset-source.ts +328 -0
  69. package/src/v5/browser.ts +139 -0
  70. package/src/v5/conversion-api.ts +287 -0
  71. package/src/v5/conversion-archive.ts +85 -0
  72. package/src/v5/core.ts +61 -0
  73. package/src/v5/feature-arrow.ts +429 -0
  74. package/src/v5/i3s-mesh-conversion.ts +192 -0
  75. package/src/v5/i3s-mesh-sink.ts +351 -0
  76. package/src/v5/i3s-mesh-source-features.ts +279 -0
  77. package/src/v5/i3s-mesh-source-material.ts +157 -0
  78. package/src/v5/i3s-mesh-source.ts +227 -0
  79. package/src/v5/index.ts +131 -0
  80. package/src/v5/mesh-conversion.ts +230 -0
  81. package/src/v5/mesh-draco.ts +182 -0
  82. package/src/v5/mesh-features.ts +319 -0
  83. package/src/v5/mesh-source-features.ts +263 -0
  84. package/src/v5/mesh-source-texture.ts +169 -0
  85. package/src/v5/mesh-source.ts +422 -0
  86. package/src/v5/mesh-tileset-archive.ts +50 -0
  87. package/src/v5/mesh-tileset-sink.ts +324 -0
  88. package/src/v5/mesh.ts +526 -0
  89. package/src/v5/point-cloud-conversion.ts +88 -0
  90. package/src/v5/point-cloud-source-encoder.ts +116 -0
  91. package/src/v5/point-cloud-source.ts +84 -0
  92. package/src/v5/point-cloud-spatial.ts +280 -0
  93. package/src/v5/point-cloud-tileset-sink.ts +234 -0
  94. package/src/v5/point-cloud.ts +398 -0
  95. package/src/v5/resource-fetcher.ts +110 -0
  96. package/src/v5/resource-manifest.ts +213 -0
  97. package/src/v5/spatial-conversion.ts +389 -0
  98. package/src/v5/tileset-source.ts +54 -0
  99. /package/src/{3d-tiles-converter → v4/3d-tiles-converter}/helpers/3d-tiles-content-converter.ts +0 -0
  100. /package/src/{3d-tiles-converter → v4/3d-tiles-converter}/helpers/i3s-obb-to-3d-tiles-obb.ts +0 -0
  101. /package/src/{3d-tiles-converter → v4/3d-tiles-converter}/helpers/load-i3s.ts +0 -0
  102. /package/src/{3d-tiles-converter → v4/3d-tiles-converter}/helpers/texture-atlas.ts +0 -0
  103. /package/src/{3d-tiles-converter → v4/3d-tiles-converter}/json-templates/tileset.ts +0 -0
  104. /package/src/{constants.ts → v4/constants.ts} +0 -0
  105. /package/src/{converter-cli.ts → v4/converter-cli.ts} +0 -0
  106. /package/src/{i3s-converter → v4/i3s-converter}/helpers/attribute-metadata-info.ts +0 -0
  107. /package/src/{i3s-converter → v4/i3s-converter}/helpers/coordinate-converter.ts +0 -0
  108. /package/src/{i3s-converter → v4/i3s-converter}/helpers/create-scene-server-path.ts +0 -0
  109. /package/src/{i3s-converter → v4/i3s-converter}/helpers/feature-attributes.ts +0 -0
  110. /package/src/{i3s-converter → v4/i3s-converter}/helpers/geometry-attributes.ts +0 -0
  111. /package/src/{i3s-converter → v4/i3s-converter}/helpers/node-debug.ts +0 -0
  112. /package/src/{i3s-converter → v4/i3s-converter}/helpers/node-index-document.ts +0 -0
  113. /package/src/{i3s-converter → v4/i3s-converter}/helpers/node-pages.ts +0 -0
  114. /package/src/{i3s-converter → v4/i3s-converter}/helpers/preprocess-3d-tiles.ts +0 -0
  115. /package/src/{i3s-converter → v4/i3s-converter}/helpers/progress.ts +0 -0
  116. /package/src/{i3s-converter → v4/i3s-converter}/helpers/tileset-traversal.ts +0 -0
  117. /package/src/{i3s-converter → v4/i3s-converter}/json-templates/geometry-definitions.ts +0 -0
  118. /package/src/{i3s-converter → v4/i3s-converter}/json-templates/layers.ts +0 -0
  119. /package/src/{i3s-converter → v4/i3s-converter}/json-templates/metadata.ts +0 -0
  120. /package/src/{i3s-converter → v4/i3s-converter}/json-templates/node.ts +0 -0
  121. /package/src/{i3s-converter → v4/i3s-converter}/json-templates/scene-server.ts +0 -0
  122. /package/src/{i3s-converter → v4/i3s-converter}/json-templates/shared-resources.ts +0 -0
  123. /package/src/{i3s-converter → v4/i3s-converter}/json-templates/store.ts +0 -0
  124. /package/src/{i3s-converter → v4/i3s-converter}/types.ts +0 -0
  125. /package/src/{i3s-server → v4/i3s-server}/app.ts +0 -0
  126. /package/src/{i3s-server → v4/i3s-server}/bin/www.ts +0 -0
  127. /package/src/{i3s-server → v4/i3s-server}/certs/cert.pem +0 -0
  128. /package/src/{i3s-server → v4/i3s-server}/certs/key.pem +0 -0
  129. /package/src/{i3s-server → v4/i3s-server}/controllers/slpk-controller.ts +0 -0
  130. /package/src/{i3s-server → v4/i3s-server}/utils/create-scene-server.ts +0 -0
  131. /package/src/{i3s-server → v4/i3s-server}/utils/server-utils.ts +0 -0
  132. /package/src/{lib → v4/lib}/json-schemas/conversion-dump-json-schema.ts +0 -0
  133. /package/src/{lib → v4/lib}/utils/cli-utils.ts +0 -0
  134. /package/src/{lib → v4/lib}/utils/conversion-dump.ts +0 -0
  135. /package/src/{lib → v4/lib}/utils/file-utils.ts +0 -0
  136. /package/src/{lib → v4/lib}/utils/geometry-utils.ts +0 -0
  137. /package/src/{lib → v4/lib}/utils/lod-conversion-utils.ts +0 -0
  138. /package/src/{lib → v4/lib}/utils/queue.ts +0 -0
  139. /package/src/{lib → v4/lib}/utils/statistic-utils.ts +0 -0
  140. /package/src/{lib → v4/lib}/utils/write-queue.ts +0 -0
  141. /package/src/{pgm-loader.ts → v4/pgm-loader.ts} +0 -0
@@ -0,0 +1,130 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {TileConversionError} from './conversion-api.js';
6
+ import type {TileConversionReport, TileConversionSink} from './conversion-api.js';
7
+
8
+ const BLOB_SIZE_GETTER = Object.getOwnPropertyDescriptor(Blob.prototype, 'size')?.get;
9
+
10
+ /** One named browser output resource. */
11
+ export interface BrowserTileConversionResource {
12
+ /** Stable relative output path supplied by the target profile. */
13
+ readonly resourceId: string;
14
+ /** Parts combined into a Blob after the total output budget is checked. */
15
+ readonly parts: readonly BlobPart[];
16
+ /** MIME type recorded on the resulting Blob. */
17
+ readonly contentType?: string;
18
+ }
19
+
20
+ /** A named output resource retained by the bounded browser sink. */
21
+ export interface BrowserTileConversionFile {
22
+ /** Stable relative output path supplied by the target profile. */
23
+ readonly resourceId: string;
24
+ /** Browser-native data for download or application-provided packaging. */
25
+ readonly blob: Blob;
26
+ }
27
+
28
+ /** Memory limit for a browser conversion sink. */
29
+ export interface BoundedMemoryTileConversionSinkOptions {
30
+ /** Maximum combined Blob size retained by this sink, captured at creation, in bytes. */
31
+ readonly maxTotalBytes: number;
32
+ }
33
+
34
+ /** Browser sink with read access to its bounded output resources. */
35
+ export interface BoundedMemoryTileConversionSink
36
+ extends TileConversionSink<BrowserTileConversionResource> {
37
+ /** Returns the retained files in stable resource-ID order. */
38
+ getFiles(): readonly BrowserTileConversionFile[];
39
+ }
40
+
41
+ /**
42
+ * Creates a browser-only sink that retains output as Blobs under a total byte limit.
43
+ *
44
+ * The sink does not download or archive files. Applications can use `getFiles()` after
45
+ * `convertTileset` completes and package or download the returned Blobs as needed.
46
+ *
47
+ * @param options - Maximum in-memory output size.
48
+ * @returns A sink that stores browser-native Blobs and clears them on abort.
49
+ */
50
+ export function createBoundedMemoryTileConversionSink(
51
+ options: BoundedMemoryTileConversionSinkOptions
52
+ ): BoundedMemoryTileConversionSink {
53
+ const {maxTotalBytes} = options;
54
+ if (!Number.isSafeInteger(maxTotalBytes) || maxTotalBytes < 0) {
55
+ throw new TileConversionError(
56
+ 'INVALID_OUTPUT_MEMORY_LIMIT',
57
+ 'The browser output memory limit must be a non-negative safe integer'
58
+ );
59
+ }
60
+
61
+ const files = new Map<string, BrowserTileConversionFile>();
62
+ let totalBytes = 0;
63
+ let closed = false;
64
+
65
+ return {
66
+ getFiles() {
67
+ return [...files.values()].sort((left, right) =>
68
+ left.resourceId < right.resourceId ? -1 : left.resourceId > right.resourceId ? 1 : 0
69
+ );
70
+ },
71
+ async write(resource, signal) {
72
+ if (signal?.aborted) {
73
+ throw signal.reason ?? new DOMException('The operation was aborted', 'AbortError');
74
+ }
75
+ if (closed) {
76
+ throw new TileConversionError(
77
+ 'RESOURCE_SINK_CLOSED',
78
+ 'Cannot write after sink finalization'
79
+ );
80
+ }
81
+ if (!resource.resourceId || files.has(resource.resourceId)) {
82
+ throw new TileConversionError(
83
+ 'INVALID_BROWSER_RESOURCE_ID',
84
+ 'Browser output resource IDs must be non-empty and unique'
85
+ );
86
+ }
87
+
88
+ const byteLength = resource.parts.reduce((total, part) => total + measureBlobPart(part), 0);
89
+ if (!Number.isSafeInteger(byteLength) || totalBytes + byteLength > maxTotalBytes) {
90
+ throw new TileConversionError(
91
+ 'OUTPUT_MEMORY_LIMIT_EXCEEDED',
92
+ `Browser output exceeds the configured ${maxTotalBytes} byte memory limit`
93
+ );
94
+ }
95
+
96
+ const blob = new Blob([...resource.parts], {type: resource.contentType});
97
+ files.set(resource.resourceId, {resourceId: resource.resourceId, blob});
98
+ totalBytes += blob.size;
99
+ },
100
+ async finalize(_report: TileConversionReport) {
101
+ if (closed) {
102
+ throw new TileConversionError('RESOURCE_SINK_CLOSED', 'Sink has already been finalized');
103
+ }
104
+ closed = true;
105
+ },
106
+ async abort() {
107
+ files.clear();
108
+ totalBytes = 0;
109
+ closed = true;
110
+ }
111
+ };
112
+ }
113
+
114
+ /** Measures supported Blob parts without copying them into a Blob first. */
115
+ function measureBlobPart(part: BlobPart): number {
116
+ if (typeof part === 'string') {
117
+ return new TextEncoder().encode(part).byteLength;
118
+ }
119
+ if (BLOB_SIZE_GETTER) {
120
+ try {
121
+ return BLOB_SIZE_GETTER.call(part) as number;
122
+ } catch {
123
+ // Keep checking the remaining supported Blob part types.
124
+ }
125
+ }
126
+ if (part instanceof ArrayBuffer || ArrayBuffer.isView(part)) {
127
+ return part.byteLength;
128
+ }
129
+ throw new TileConversionError('UNSUPPORTED_BROWSER_RESOURCE', 'Unsupported browser Blob part');
130
+ }
@@ -0,0 +1,155 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {TileConversionError} from './conversion-api.js';
6
+ import type {TileConversionSource} from './conversion-api.js';
7
+
8
+ /** Metadata available before a browser resource is read. */
9
+ export interface BrowserTileConversionInspection {
10
+ /** Stable resource path assigned to the input tile. */
11
+ readonly resourceId: string;
12
+ /** Optional MIME type supplied by the caller or source Blob. */
13
+ readonly contentType?: string;
14
+ }
15
+
16
+ /** Raw bytes read from one browser tile resource. */
17
+ export interface BrowserTileConversionInputResource {
18
+ /** Stable resource path assigned to the input tile. */
19
+ readonly resourceId: string;
20
+ /** Complete resource bytes for a profile-specific codec. */
21
+ readonly data: Uint8Array;
22
+ /** Optional MIME type supplied by the caller, Blob, or URL response. */
23
+ readonly contentType?: string;
24
+ }
25
+
26
+ /** Browser input and size limit for a single-resource conversion. */
27
+ export interface BrowserTileConversionSourceOptions {
28
+ /** Input URL or already available browser Blob. */
29
+ readonly input: string | Blob;
30
+ /** Stable path to associate with the input resource. */
31
+ readonly resourceId: string;
32
+ /** Maximum number of input bytes held in memory. */
33
+ readonly maxInputBytes: number;
34
+ /** Optional MIME type override. */
35
+ readonly contentType?: string;
36
+ }
37
+
38
+ /**
39
+ * Creates a browser source for one mesh resource provided as a URL or Blob.
40
+ *
41
+ * URL responses are read incrementally and canceled if they exceed `maxInputBytes`.
42
+ * Tileset JSON traversal and format decoding remain profile-specific source and codec work.
43
+ *
44
+ * @param options - Input, stable resource ID, and maximum input size.
45
+ * @returns A source that yields one complete resource to a v5 codec.
46
+ */
47
+ export function createBrowserTileConversionSource(
48
+ options: BrowserTileConversionSourceOptions
49
+ ): TileConversionSource<BrowserTileConversionInspection, BrowserTileConversionInputResource> {
50
+ if (
51
+ !options.resourceId ||
52
+ !Number.isSafeInteger(options.maxInputBytes) ||
53
+ options.maxInputBytes < 0
54
+ ) {
55
+ throw new TileConversionError(
56
+ 'INVALID_BROWSER_SOURCE_OPTIONS',
57
+ 'Browser sources require a resource ID and a non-negative safe input byte limit'
58
+ );
59
+ }
60
+
61
+ return {
62
+ async inspect(signal) {
63
+ throwIfAborted(signal);
64
+ const contentType = options.contentType ?? getBlobContentType(options.input);
65
+ return {
66
+ resourceId: options.resourceId,
67
+ ...(contentType ? {contentType} : {})
68
+ };
69
+ },
70
+ async *read(inspection, signal) {
71
+ throwIfAborted(signal);
72
+ const input = options.input;
73
+ let data: Uint8Array;
74
+ let contentType = inspection.contentType;
75
+
76
+ if (typeof input === 'string') {
77
+ const response = await fetch(input, {signal});
78
+ if (!response.ok) {
79
+ throw new TileConversionError(
80
+ 'BROWSER_SOURCE_FETCH_FAILED',
81
+ `Could not fetch browser input (${response.status} ${response.statusText})`
82
+ );
83
+ }
84
+ contentType = contentType ?? response.headers.get('content-type') ?? undefined;
85
+ data = await readResponseBytes(response, options.maxInputBytes, signal);
86
+ } else {
87
+ if (input.size > options.maxInputBytes) {
88
+ throw createInputLimitError(options.maxInputBytes);
89
+ }
90
+ data = new Uint8Array(await input.arrayBuffer());
91
+ }
92
+
93
+ throwIfAborted(signal);
94
+ yield {
95
+ resourceId: inspection.resourceId,
96
+ data,
97
+ ...(contentType ? {contentType} : {})
98
+ };
99
+ }
100
+ };
101
+ }
102
+
103
+ /** Reads a response stream while enforcing the input byte limit. */
104
+ async function readResponseBytes(
105
+ response: Response,
106
+ maxInputBytes: number,
107
+ signal?: AbortSignal
108
+ ): Promise<Uint8Array> {
109
+ const reader = response.body?.getReader();
110
+ if (!reader) {
111
+ throw new TileConversionError(
112
+ 'BROWSER_SOURCE_STREAM_UNAVAILABLE',
113
+ 'Browser URL inputs must provide a readable response stream'
114
+ );
115
+ }
116
+
117
+ const data = new Uint8Array(maxInputBytes);
118
+ let totalBytes = 0;
119
+ try {
120
+ while (true) {
121
+ throwIfAborted(signal);
122
+ const {done, value} = await reader.read();
123
+ if (done) break;
124
+ if (value.byteLength > maxInputBytes - totalBytes) {
125
+ await reader.cancel();
126
+ throw createInputLimitError(maxInputBytes);
127
+ }
128
+ data.set(value, totalBytes);
129
+ totalBytes += value.byteLength;
130
+ }
131
+ } finally {
132
+ reader.releaseLock();
133
+ }
134
+ return data.subarray(0, totalBytes);
135
+ }
136
+
137
+ /** Returns a Blob's MIME type without requiring a same-realm Blob constructor. */
138
+ function getBlobContentType(input: string | Blob): string | undefined {
139
+ return typeof input === 'string' ? undefined : input.type || undefined;
140
+ }
141
+
142
+ /** Creates the typed error used when browser input exceeds its configured limit. */
143
+ function createInputLimitError(maxInputBytes: number): TileConversionError {
144
+ return new TileConversionError(
145
+ 'INPUT_RESOURCE_TOO_LARGE',
146
+ `Browser input exceeds the configured ${maxInputBytes} byte memory limit`
147
+ );
148
+ }
149
+
150
+ /** Throws the signal reason when a browser source operation has been canceled. */
151
+ function throwIfAborted(signal?: AbortSignal): void {
152
+ if (signal?.aborted) {
153
+ throw signal.reason ?? new DOMException('The operation was aborted', 'AbortError');
154
+ }
155
+ }
@@ -0,0 +1,328 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {TileConversionError} from './conversion-api.js';
6
+ import type {TileConversionSource} from './conversion-api.js';
7
+
8
+ type TilesetContent = {uri?: string; url?: string};
9
+ type TilesetTile = {
10
+ content?: TilesetContent;
11
+ contents?: TilesetContent[];
12
+ children?: TilesetTile[];
13
+ transform?: number[];
14
+ implicitTiling?: unknown;
15
+ extensions?: Record<string, unknown>;
16
+ };
17
+ type TilesetDocument = {asset?: {version?: string}; root?: TilesetTile};
18
+
19
+ /** Input options for bounded traversal of one explicit 3D Tiles tileset. */
20
+ export interface BrowserTilesetConversionSourceOptions {
21
+ /** URL of the root tileset JSON document. */
22
+ readonly input: string;
23
+ /** Maximum bytes retained across the tileset JSON and all content resources. */
24
+ readonly maxInputBytes: number;
25
+ /** Maximum number of content resources to visit. */
26
+ readonly maxInputResources: number;
27
+ /** Fetch implementation, defaulting to the browser global. */
28
+ readonly fetcher?: typeof fetch;
29
+ }
30
+
31
+ /** Inspection result for an explicit 3D Tiles source. */
32
+ export interface BrowserTilesetConversionInspection {
33
+ /** Absolute URL of the root tileset document. */
34
+ readonly rootUrl: string;
35
+ /** Parsed root document used for deterministic content traversal. */
36
+ readonly tileset: TilesetDocument;
37
+ /** Ordered content descriptors. */
38
+ readonly resources: readonly BrowserTilesetResourceDescriptor[];
39
+ /** Root document bytes charged against the input limit. */
40
+ readonly tilesetBytes: number;
41
+ }
42
+
43
+ /** Placement metadata for one ordered content resource. */
44
+ export interface BrowserTilesetResourceDescriptor {
45
+ /** Unique, stable path for this content placement. */
46
+ readonly resourceId: string;
47
+ /** Original URI declared by the tileset. */
48
+ readonly uri: string;
49
+ /** Tile child indexes from the tileset root. */
50
+ readonly tilePath: readonly number[];
51
+ /** Combined root-to-tile transform in column-major order. */
52
+ readonly transform: readonly number[];
53
+ }
54
+
55
+ /** One raw content resource from a browser-readable explicit tileset. */
56
+ export interface BrowserTilesetConversionInputResource extends BrowserTilesetResourceDescriptor {
57
+ /** Complete content bytes for an injected profile-specific codec. */
58
+ readonly data: Uint8Array;
59
+ /** Optional content type reported by the response. */
60
+ readonly contentType?: string;
61
+ }
62
+
63
+ /**
64
+ * Creates a bounded browser source for explicit 3D Tiles 1.0 and 1.1 tilesets.
65
+ *
66
+ * Traversal visits each declared content placement once in depth-first order. Implicit tiling and
67
+ * nested external tilesets are rejected; content decoding and conversion remain codec work.
68
+ *
69
+ * @param options - Tileset URL and aggregate byte and resource limits.
70
+ * @returns A source yielding ordered raw tile resources with placement transforms.
71
+ */
72
+ export function createBrowserTilesetConversionSource(
73
+ options: BrowserTilesetConversionSourceOptions
74
+ ): TileConversionSource<BrowserTilesetConversionInspection, BrowserTilesetConversionInputResource> {
75
+ if (
76
+ !Number.isSafeInteger(options.maxInputBytes) ||
77
+ options.maxInputBytes < 1 ||
78
+ !Number.isSafeInteger(options.maxInputResources) ||
79
+ options.maxInputResources < 1
80
+ ) {
81
+ throw new TileConversionError(
82
+ 'INVALID_BROWSER_SOURCE_OPTIONS',
83
+ 'Browser tileset sources require positive byte and resource limits'
84
+ );
85
+ }
86
+
87
+ const fetcher = options.fetcher ?? fetch;
88
+ return {
89
+ async inspect(signal) {
90
+ throwIfAborted(signal);
91
+ const rootUrl = new URL(options.input).href;
92
+ const response = await fetcher(rootUrl, {signal});
93
+ if (!response.ok) {
94
+ throw new TileConversionError(
95
+ 'BROWSER_SOURCE_FETCH_FAILED',
96
+ `Could not fetch browser tileset (${response.status} ${response.statusText})`
97
+ );
98
+ }
99
+ const data = await readResponseBytes(response, options.maxInputBytes, signal);
100
+ let tileset: TilesetDocument;
101
+ try {
102
+ tileset = JSON.parse(new TextDecoder().decode(data)) as TilesetDocument;
103
+ } catch {
104
+ throw new TileConversionError('INVALID_BROWSER_TILESET', 'Tileset input is not valid JSON');
105
+ }
106
+ if (
107
+ !isRecord(tileset) ||
108
+ !['1.0', '1.1'].includes(tileset.asset?.version || '') ||
109
+ !isRecord(tileset.root)
110
+ ) {
111
+ throw new TileConversionError(
112
+ 'UNSUPPORTED_BROWSER_TILESET',
113
+ 'Browser tileset traversal supports explicit 3D Tiles 1.0 and 1.1 documents'
114
+ );
115
+ }
116
+ const resources = collectResources(tileset.root, options.maxInputResources);
117
+ return {rootUrl, tileset, resources, tilesetBytes: data.byteLength};
118
+ },
119
+ async *read(inspection, signal) {
120
+ let inputBytes = inspection.tilesetBytes;
121
+ for (const descriptor of inspection.resources) {
122
+ throwIfAborted(signal);
123
+ const resourceUrl = resolveResourceUrl(descriptor.uri, inspection.rootUrl);
124
+ const response = await fetcher(resourceUrl, {signal});
125
+ if (!response.ok) {
126
+ throw new TileConversionError(
127
+ 'BROWSER_SOURCE_FETCH_FAILED',
128
+ `Could not fetch browser tile content (${response.status} ${response.statusText})`,
129
+ [
130
+ {
131
+ code: 'BROWSER_SOURCE_FETCH_FAILED',
132
+ message: resourceUrl,
133
+ severity: 'error',
134
+ resourceId: descriptor.resourceId
135
+ }
136
+ ]
137
+ );
138
+ }
139
+ const remainingBytes = options.maxInputBytes - inputBytes;
140
+ const data = await readResponseBytes(response, remainingBytes, signal);
141
+ inputBytes += data.byteLength;
142
+ const contentType = response.headers.get('content-type') ?? undefined;
143
+ if (isJsonContent(data, contentType)) {
144
+ throw new TileConversionError(
145
+ 'UNSUPPORTED_BROWSER_TILESET_CONTENT',
146
+ `Nested tilesets and JSON tile content are not supported: ${descriptor.uri}`
147
+ );
148
+ }
149
+ yield {...descriptor, data, ...(contentType ? {contentType} : {})};
150
+ }
151
+ }
152
+ };
153
+ }
154
+
155
+ /** Collects explicit tile contents in declaration order and fails on unbounded formats. */
156
+ function collectResources(
157
+ root: TilesetTile,
158
+ maxInputResources: number
159
+ ): BrowserTilesetResourceDescriptor[] {
160
+ const resources: BrowserTilesetResourceDescriptor[] = [];
161
+ const stack: Array<{tile: TilesetTile; path: number[]; transform: number[]}> = [
162
+ {tile: root, path: [], transform: identityMatrix()}
163
+ ];
164
+ while (stack.length > 0) {
165
+ const {tile, path, transform: parentTransform} = stack.pop()!;
166
+ if (!isRecord(tile)) {
167
+ throw new TileConversionError('INVALID_BROWSER_TILESET', 'Tile entries must be objects');
168
+ }
169
+ if (
170
+ tile.implicitTiling !== undefined ||
171
+ tile.extensions?.['3DTILES_implicit_tiling'] !== undefined
172
+ ) {
173
+ throw new TileConversionError(
174
+ 'UNSUPPORTED_BROWSER_TILESET',
175
+ 'Browser tileset traversal does not yet support implicit tiling'
176
+ );
177
+ }
178
+ if (tile.contents !== undefined && !Array.isArray(tile.contents)) {
179
+ throw new TileConversionError('INVALID_BROWSER_TILESET', 'Tile contents must be an array');
180
+ }
181
+ if (tile.children !== undefined && !Array.isArray(tile.children)) {
182
+ throw new TileConversionError('INVALID_BROWSER_TILESET', 'Tile children must be an array');
183
+ }
184
+ const transform = multiplyMatrices(parentTransform, normalizeMatrix(tile.transform));
185
+ const contents = tile.contents ?? (tile.content ? [tile.content] : []);
186
+ for (let contentIndex = 0; contentIndex < contents.length; contentIndex++) {
187
+ const content = contents[contentIndex];
188
+ if (!isRecord(content)) {
189
+ throw new TileConversionError(
190
+ 'INVALID_BROWSER_TILESET',
191
+ 'Tile content entries must be objects'
192
+ );
193
+ }
194
+ const uri = content.uri ?? content.url;
195
+ if (typeof uri !== 'string' || !uri) {
196
+ throw new TileConversionError(
197
+ 'INVALID_BROWSER_TILESET',
198
+ `Tile content at ${path.join('/')} has no URI`
199
+ );
200
+ }
201
+ if (resources.length >= maxInputResources) {
202
+ throw new TileConversionError(
203
+ 'INPUT_RESOURCE_COUNT_EXCEEDED',
204
+ `Browser tileset exceeds the configured ${maxInputResources} resource limit`
205
+ );
206
+ }
207
+ resources.push({
208
+ resourceId: `tile-${path.length ? path.join('-') : 'root'}-content-${contentIndex}`,
209
+ uri,
210
+ tilePath: path,
211
+ transform
212
+ });
213
+ }
214
+ const children = tile.children ?? [];
215
+ for (let childIndex = children.length - 1; childIndex >= 0; childIndex--) {
216
+ stack.push({
217
+ tile: children[childIndex],
218
+ path: [...path, childIndex],
219
+ transform
220
+ });
221
+ }
222
+ }
223
+ return resources;
224
+ }
225
+
226
+ /** Reads one response into a single buffer without exceeding its remaining byte budget. */
227
+ async function readResponseBytes(
228
+ response: Response,
229
+ maximumBytes: number,
230
+ signal?: AbortSignal
231
+ ): Promise<Uint8Array> {
232
+ const reader = response.body?.getReader();
233
+ if (!reader) {
234
+ throw new TileConversionError(
235
+ 'BROWSER_SOURCE_STREAM_UNAVAILABLE',
236
+ 'Browser inputs must provide a readable response stream'
237
+ );
238
+ }
239
+ const data = new Uint8Array(maximumBytes);
240
+ let byteLength = 0;
241
+ try {
242
+ while (true) {
243
+ throwIfAborted(signal);
244
+ const {done, value} = await reader.read();
245
+ if (done) break;
246
+ if (value.byteLength > maximumBytes - byteLength) {
247
+ await reader.cancel();
248
+ throw new TileConversionError(
249
+ 'INPUT_RESOURCE_TOO_LARGE',
250
+ `Browser tileset input exceeds the configured ${maximumBytes} byte remaining limit`
251
+ );
252
+ }
253
+ data.set(value, byteLength);
254
+ byteLength += value.byteLength;
255
+ }
256
+ } finally {
257
+ reader.releaseLock();
258
+ }
259
+ return data.subarray(0, byteLength);
260
+ }
261
+
262
+ /** Detects JSON payloads that may declare an unsupported external tileset. */
263
+ function isJsonContent(data: Uint8Array, contentType?: string): boolean {
264
+ if (contentType?.includes('json')) return true;
265
+ let byteOffset = data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf ? 3 : 0;
266
+ while ([0x09, 0x0a, 0x0d, 0x20].includes(data[byteOffset])) byteOffset++;
267
+ return data[byteOffset] === 0x7b;
268
+ }
269
+
270
+ /** Resolves a tile URI and carries root query parameters to same-origin relative resources. */
271
+ function resolveResourceUrl(uri: string, rootUrl: string): string {
272
+ const root = new URL(rootUrl);
273
+ const resource = new URL(uri, root);
274
+ if (resource.origin === root.origin) {
275
+ for (const [name, value] of root.searchParams) {
276
+ if (!resource.searchParams.has(name)) resource.searchParams.append(name, value);
277
+ }
278
+ }
279
+ return resource.href;
280
+ }
281
+
282
+ /** Returns whether a JSON value is a non-array object. */
283
+ function isRecord(value: unknown): value is Record<string, unknown> {
284
+ return Boolean(value && typeof value === 'object' && !Array.isArray(value));
285
+ }
286
+
287
+ /** Returns an identity matrix in column-major order. */
288
+ function identityMatrix(): number[] {
289
+ return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
290
+ }
291
+
292
+ /** Validates and copies a tile's optional column-major transform. */
293
+ function normalizeMatrix(matrix?: number[]): number[] {
294
+ if (matrix === undefined) return identityMatrix();
295
+ if (
296
+ !Array.isArray(matrix) ||
297
+ matrix.length !== 16 ||
298
+ matrix.some(value => !Number.isFinite(value))
299
+ ) {
300
+ throw new TileConversionError(
301
+ 'INVALID_BROWSER_TILESET',
302
+ 'Tile transform must contain 16 finite numbers'
303
+ );
304
+ }
305
+ return matrix;
306
+ }
307
+
308
+ /** Multiplies column-major 4x4 matrices. */
309
+ function multiplyMatrices(left: number[], right: number[]): number[] {
310
+ const result = new Array<number>(16);
311
+ for (let column = 0; column < 4; column++) {
312
+ for (let row = 0; row < 4; row++) {
313
+ result[column * 4 + row] =
314
+ left[row] * right[column * 4] +
315
+ left[4 + row] * right[column * 4 + 1] +
316
+ left[8 + row] * right[column * 4 + 2] +
317
+ left[12 + row] * right[column * 4 + 3];
318
+ }
319
+ }
320
+ return result;
321
+ }
322
+
323
+ /** Throws the signal reason when a browser source operation has been canceled. */
324
+ function throwIfAborted(signal?: AbortSignal): void {
325
+ if (signal?.aborted) {
326
+ throw signal.reason ?? new DOMException('The operation was aborted', 'AbortError');
327
+ }
328
+ }