MCPcopy Create free account
hub / github.com/ColmapView/Colmapview.github.io / encodeShareData

Function encodeShareData

src/utils/shareDataCodec.ts:137–203  ·  view source on GitHub ↗
(
  manifestUrlOrManifest: string | ColmapManifest,
  viewState: CameraViewState | null,
  config?: ShareConfig | null
)

Source from the content-addressed store, hash-verified

135 * bit 4 = 32-bit little-endian length fields instead of legacy 16-bit lengths.
136 */
137export function encodeShareData(
138 manifestUrlOrManifest: string | ColmapManifest,
139 viewState: CameraViewState | null,
140 config?: ShareConfig | null
141): string {
142 const isInlineManifest = typeof manifestUrlOrManifest !== 'string';
143 const dataString = isInlineManifest
144 ? JSON.stringify(manifestUrlOrManifest)
145 : manifestUrlOrManifest;
146 const dataBytes = new TextEncoder().encode(dataString);
147 const dataLength = dataBytes.length;
148
149 const hasCamera = viewState !== null;
150 const hasConfig = config !== null && config !== undefined;
151 const configBytes = hasConfig ? new TextEncoder().encode(JSON.stringify(config)) : null;
152 const configLength = configBytes?.length ?? 0;
153 const usesWideLengths = dataLength > UINT16_MAX || configLength > UINT16_MAX;
154 const lengthFieldBytes = getLengthFieldBytes(usesWideLengths);
155
156 const payloadLength = lengthFieldBytes + dataLength + (hasCamera ? 72 : 0) + (hasConfig ? lengthFieldBytes + configLength : 0);
157 const payloadBuffer = new ArrayBuffer(payloadLength);
158 const payloadView = new DataView(payloadBuffer);
159 const payloadBytes = new Uint8Array(payloadBuffer);
160
161 let offset = 0;
162 offset = writeShareDataLength(payloadView, offset, dataLength, lengthFieldBytes);
163
164 payloadBytes.set(dataBytes, offset);
165 offset += dataLength;
166
167 if (hasCamera && viewState) {
168 payloadBytes.set(encodeCameraStateBinary(viewState), offset);
169 offset += 72;
170 }
171
172 if (hasConfig && configBytes) {
173 offset = writeShareDataLength(payloadView, offset, configLength, lengthFieldBytes);
174 payloadBytes.set(configBytes, offset);
175 }
176
177 let finalPayload: Uint8Array = payloadBytes;
178 let isCompressed = false;
179
180 if (deflateSync) {
181 try {
182 const compressed = deflateSync(payloadBytes, { level: 9 });
183 if (compressed.length < payloadBytes.length) {
184 finalPayload = compressed;
185 isCompressed = true;
186 }
187 } catch {
188 finalPayload = payloadBytes;
189 }
190 }
191
192 const flags =
193 (hasCamera ? SHARE_DATA_FLAG_CAMERA : 0) |
194 (hasConfig ? SHARE_DATA_FLAG_CONFIG : 0) |

Callers 3

buildShareableUrlFunction · 0.90

Calls 4

encodeCameraStateBinaryFunction · 0.90
toBase64UrlFunction · 0.90
getLengthFieldBytesFunction · 0.85
writeShareDataLengthFunction · 0.85

Tested by

no test coverage detected