maplibre-tilepackage-protocol 1.2.0 → 1.2.1

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/dist/index.esm.js CHANGED
@@ -316,9 +316,18 @@ class FetchSource extends Source {
316
316
  this.url = url;
317
317
  this.customHeaders = customHeaders;
318
318
  this.mustReload = false;
319
+ // Safe global detection for legacy environments without globalThis
320
+ const g =
321
+ typeof globalThis !== "undefined"
322
+ ? globalThis
323
+ : typeof window !== "undefined"
324
+ ? window
325
+ : typeof self !== "undefined"
326
+ ? self
327
+ : {};
319
328
  let userAgent = "";
320
- if ("navigator" in globalThis) {
321
- userAgent = globalThis.navigator.userAgent || "";
329
+ if (g && typeof g.navigator !== "undefined") {
330
+ userAgent = g.navigator.userAgent || "";
322
331
  }
323
332
  const isWindows = userAgent.indexOf("Windows") > -1;
324
333
  const isChromiumBased = /Chrome|Chromium|Edg|OPR|Brave/.test(userAgent);
@@ -1187,16 +1196,23 @@ async function defaultDecompress(buf, compression) {
1187
1196
  return buf;
1188
1197
  }
1189
1198
  if (compression === "gzip") {
1190
- if (typeof globalThis.DecompressionStream === "undefined") {
1199
+ // Safe global detection for DecompressionStream feature test
1200
+ const g =
1201
+ typeof globalThis !== "undefined"
1202
+ ? globalThis
1203
+ : typeof window !== "undefined"
1204
+ ? window
1205
+ : typeof self !== "undefined"
1206
+ ? self
1207
+ : {};
1208
+ if (typeof g.DecompressionStream === "undefined") {
1191
1209
  return decompressSync(new Uint8Array(buf));
1192
1210
  }
1193
1211
  const stream = new Response(buf).body;
1194
1212
  if (!stream) {
1195
1213
  throw new Error("Failed to read response stream");
1196
1214
  }
1197
- const result = stream.pipeThrough(
1198
- new globalThis.DecompressionStream("gzip"),
1199
- );
1215
+ const result = stream.pipeThrough(new g.DecompressionStream("gzip"));
1200
1216
  return new Response(result).arrayBuffer();
1201
1217
  }
1202
1218
  throw new Error("Compression method not supported");
@@ -1717,13 +1733,23 @@ class SharedPromiseCache {
1717
1733
  }
1718
1734
  }
1719
1735
 
1736
+ /**
1737
+ * Escape literal newlines in JSON text.
1738
+ * Replaces actual newline characters with the JSON escape sequence.
1739
+ */
1740
+ function escapeJsonNewlines(text) {
1741
+ // Replace literal newlines with JSON escape sequences
1742
+ // This handles \r\n, \n, and \r
1743
+ return text.replace(/\r\n/g, "\\n").replace(/[\r\n]/g, "\\n");
1744
+ }
1745
+
1720
1746
  function calculateFilename(z, x, y, header) {
1721
1747
  const zoom = z.toString().padStart(2, "0");
1722
1748
  const baseRow = Math.floor(y / 128) * 128;
1723
1749
  const baseCol = Math.floor(x / 128) * 128;
1724
1750
  const rowHex = baseRow.toString(16).padStart(4, "0");
1725
1751
  const colHex = baseCol.toString(16).padStart(4, "0");
1726
- let basePath = header.type === "tpkx" ? "tile" : "p12/tile";
1752
+ const basePath = header.type === "tpkx" ? "tile" : "p12/tile";
1727
1753
  return `${basePath}/L${zoom}/R${rowHex}C${colHex}.bundle`;
1728
1754
  }
1729
1755
 
@@ -1883,13 +1909,16 @@ class TilePackage {
1883
1909
  let metadata = {};
1884
1910
  if (header.packageType === "vtpk") {
1885
1911
  const resp = await this.source.getBytes(
1886
- header.jsonMetadataOffset,
1887
- header.jsonMetadataLength,
1912
+ header.metadataOffset,
1913
+ header.metadataLength,
1888
1914
  undefined,
1889
1915
  header.etag,
1890
1916
  );
1891
1917
  const decoder = new TextDecoder("utf-8");
1892
- metadata = JSON.parse(decoder.decode(resp.data));
1918
+ const jsonText = decoder.decode(resp.data);
1919
+ console.log("test", jsonText);
1920
+ console.log("escaped text", escapeJsonNewlines(jsonText));
1921
+ metadata = JSON.parse(escapeJsonNewlines(jsonText));
1893
1922
  }
1894
1923
  metadata.name = header.name;
1895
1924
  return metadata;