@tileflow/core 0.1.0-alpha.20 → 0.1.0-alpha.22
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/GENERATED_OUTPUT_LICENSE.md +21 -0
- package/LICENSE +201 -0
- package/NOTICE +9 -0
- package/README.md +1097 -145
- package/THIRD_PARTY_NOTICES.md +243 -0
- package/TRADEMARKS.md +10 -0
- package/dist/browser.d.ts +182 -13
- package/dist/browser.js +4207 -116
- package/dist/build.d.ts +158 -0
- package/dist/build.js +14836 -0
- package/dist/capture-scene-BK_0J2oR.d.ts +161 -0
- package/dist/capture.d.ts +9 -0
- package/dist/capture.js +206 -0
- package/dist/config-reference.json +31882 -0
- package/dist/index.d.ts +643 -182
- package/dist/index.js +15943 -4352
- package/dist/manifest-B5GKVMFE.d.ts +757 -0
- package/dist/manifest.d.ts +2 -0
- package/dist/manifest.js +245 -0
- package/dist/map-BHL_0Mkx.d.ts +1145 -0
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.js +1172 -0
- package/package.json +37 -5
- package/dist/runtime-DroKDIuB.d.ts +0 -1069
package/dist/browser.js
CHANGED
|
@@ -1,90 +1,4184 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __esm = (fn, res, err2) => function __init() {
|
|
4
|
+
if (err2) throw err2[0];
|
|
5
|
+
try {
|
|
6
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
7
|
+
} catch (e) {
|
|
8
|
+
throw err2 = [e], e;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// src/portable-identity-rules.ts
|
|
17
|
+
function isTileflowPortableId(value) {
|
|
18
|
+
return typeof value === "string" && value.length <= tileflowPortableIdMaximumLength && tileflowPortableIdPattern.test(value) && !["constructor", "prototype"].includes(value) && !/^(?:AUX|CON|NUL|PRN|COM[1-9]|LPT[1-9])$/i.test(value);
|
|
19
|
+
}
|
|
20
|
+
function isTileflowThemeName(value) {
|
|
21
|
+
return isTileflowPortableId(value) && value !== "system";
|
|
22
|
+
}
|
|
23
|
+
var tileflowPortableIdMaximumLength, tileflowPortableIdPattern;
|
|
24
|
+
var init_portable_identity_rules = __esm({
|
|
25
|
+
"src/portable-identity-rules.ts"() {
|
|
26
|
+
"use strict";
|
|
27
|
+
tileflowPortableIdMaximumLength = 64;
|
|
28
|
+
tileflowPortableIdPattern = /^[a-z][a-z0-9-]{0,63}$/u;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// ../../node_modules/.pnpm/maplibre-contour@0.1.0/node_modules/maplibre-contour/dist/index.mjs
|
|
33
|
+
var dist_exports = {};
|
|
34
|
+
__export(dist_exports, {
|
|
35
|
+
default: () => mlcontour$1
|
|
36
|
+
});
|
|
37
|
+
function define(_2, chunk) {
|
|
38
|
+
if (!shared) {
|
|
39
|
+
shared = chunk;
|
|
40
|
+
} else if (!worker) {
|
|
41
|
+
worker = chunk;
|
|
42
|
+
} else {
|
|
43
|
+
var workerBundleString = "var sharedChunk = {}; (" + shared + ")(sharedChunk); (" + worker + ")(sharedChunk);";
|
|
44
|
+
var sharedChunk = {};
|
|
45
|
+
shared(sharedChunk);
|
|
46
|
+
mlcontour = chunk(sharedChunk);
|
|
47
|
+
if (typeof window !== "undefined") {
|
|
48
|
+
mlcontour.workerUrl = window.URL.createObjectURL(
|
|
49
|
+
new Blob([workerBundleString], { type: "text/javascript" })
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
var shared, worker, mlcontour, mlcontour$1;
|
|
55
|
+
var init_dist = __esm({
|
|
56
|
+
"../../node_modules/.pnpm/maplibre-contour@0.1.0/node_modules/maplibre-contour/dist/index.mjs"() {
|
|
57
|
+
"use strict";
|
|
58
|
+
define(["exports"], (function(exports) {
|
|
59
|
+
"use strict";
|
|
60
|
+
class Fragment {
|
|
61
|
+
constructor(start, end) {
|
|
62
|
+
this.start = start;
|
|
63
|
+
this.end = end;
|
|
64
|
+
this.points = [];
|
|
65
|
+
this.append = this.append.bind(this);
|
|
66
|
+
this.prepend = this.prepend.bind(this);
|
|
67
|
+
}
|
|
68
|
+
append(x3, y) {
|
|
69
|
+
this.points.push(Math.round(x3), Math.round(y));
|
|
70
|
+
}
|
|
71
|
+
prepend(x3, y) {
|
|
72
|
+
this.points.splice(0, 0, Math.round(x3), Math.round(y));
|
|
73
|
+
}
|
|
74
|
+
lineString() {
|
|
75
|
+
return this.toArray();
|
|
76
|
+
}
|
|
77
|
+
isEmpty() {
|
|
78
|
+
return this.points.length < 2;
|
|
79
|
+
}
|
|
80
|
+
appendFragment(other) {
|
|
81
|
+
this.points.push(...other.points);
|
|
82
|
+
this.end = other.end;
|
|
83
|
+
}
|
|
84
|
+
toArray() {
|
|
85
|
+
return this.points;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const CASES = [
|
|
89
|
+
[],
|
|
90
|
+
[
|
|
91
|
+
[
|
|
92
|
+
[1, 2],
|
|
93
|
+
[0, 1]
|
|
94
|
+
]
|
|
95
|
+
],
|
|
96
|
+
[
|
|
97
|
+
[
|
|
98
|
+
[2, 1],
|
|
99
|
+
[1, 2]
|
|
100
|
+
]
|
|
101
|
+
],
|
|
102
|
+
[
|
|
103
|
+
[
|
|
104
|
+
[2, 1],
|
|
105
|
+
[0, 1]
|
|
106
|
+
]
|
|
107
|
+
],
|
|
108
|
+
[
|
|
109
|
+
[
|
|
110
|
+
[1, 0],
|
|
111
|
+
[2, 1]
|
|
112
|
+
]
|
|
113
|
+
],
|
|
114
|
+
[
|
|
115
|
+
[
|
|
116
|
+
[1, 2],
|
|
117
|
+
[0, 1]
|
|
118
|
+
],
|
|
119
|
+
[
|
|
120
|
+
[1, 0],
|
|
121
|
+
[2, 1]
|
|
122
|
+
]
|
|
123
|
+
],
|
|
124
|
+
[
|
|
125
|
+
[
|
|
126
|
+
[1, 0],
|
|
127
|
+
[1, 2]
|
|
128
|
+
]
|
|
129
|
+
],
|
|
130
|
+
[
|
|
131
|
+
[
|
|
132
|
+
[1, 0],
|
|
133
|
+
[0, 1]
|
|
134
|
+
]
|
|
135
|
+
],
|
|
136
|
+
[
|
|
137
|
+
[
|
|
138
|
+
[0, 1],
|
|
139
|
+
[1, 0]
|
|
140
|
+
]
|
|
141
|
+
],
|
|
142
|
+
[
|
|
143
|
+
[
|
|
144
|
+
[1, 2],
|
|
145
|
+
[1, 0]
|
|
146
|
+
]
|
|
147
|
+
],
|
|
148
|
+
[
|
|
149
|
+
[
|
|
150
|
+
[0, 1],
|
|
151
|
+
[1, 0]
|
|
152
|
+
],
|
|
153
|
+
[
|
|
154
|
+
[2, 1],
|
|
155
|
+
[1, 2]
|
|
156
|
+
]
|
|
157
|
+
],
|
|
158
|
+
[
|
|
159
|
+
[
|
|
160
|
+
[2, 1],
|
|
161
|
+
[1, 0]
|
|
162
|
+
]
|
|
163
|
+
],
|
|
164
|
+
[
|
|
165
|
+
[
|
|
166
|
+
[0, 1],
|
|
167
|
+
[2, 1]
|
|
168
|
+
]
|
|
169
|
+
],
|
|
170
|
+
[
|
|
171
|
+
[
|
|
172
|
+
[1, 2],
|
|
173
|
+
[2, 1]
|
|
174
|
+
]
|
|
175
|
+
],
|
|
176
|
+
[
|
|
177
|
+
[
|
|
178
|
+
[0, 1],
|
|
179
|
+
[1, 2]
|
|
180
|
+
]
|
|
181
|
+
],
|
|
182
|
+
[]
|
|
183
|
+
];
|
|
184
|
+
function index(width, x3, y, point) {
|
|
185
|
+
x3 = x3 * 2 + point[0];
|
|
186
|
+
y = y * 2 + point[1];
|
|
187
|
+
return x3 + y * (width + 1) * 2;
|
|
188
|
+
}
|
|
189
|
+
function ratio(a, b2, c) {
|
|
190
|
+
return (b2 - a) / (c - a);
|
|
191
|
+
}
|
|
192
|
+
function generateIsolines(interval, tile, extent = 4096, buffer = 1) {
|
|
193
|
+
if (!interval) {
|
|
194
|
+
return {};
|
|
195
|
+
}
|
|
196
|
+
const multiplier = extent / (tile.width - 1);
|
|
197
|
+
let tld, trd, bld, brd;
|
|
198
|
+
let r, c;
|
|
199
|
+
const segments = {};
|
|
200
|
+
const fragmentByStartByLevel = /* @__PURE__ */ new Map();
|
|
201
|
+
const fragmentByEndByLevel = /* @__PURE__ */ new Map();
|
|
202
|
+
function interpolate(point, threshold, accept) {
|
|
203
|
+
if (point[0] === 0) {
|
|
204
|
+
accept(multiplier * (c - 1), multiplier * (r - ratio(bld, threshold, tld)));
|
|
205
|
+
} else if (point[0] === 2) {
|
|
206
|
+
accept(multiplier * c, multiplier * (r - ratio(brd, threshold, trd)));
|
|
207
|
+
} else if (point[1] === 0) {
|
|
208
|
+
accept(multiplier * (c - ratio(trd, threshold, tld)), multiplier * (r - 1));
|
|
209
|
+
} else {
|
|
210
|
+
accept(multiplier * (c - ratio(brd, threshold, bld)), multiplier * r);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
for (r = 1 - buffer; r < tile.height + buffer; r++) {
|
|
214
|
+
trd = tile.get(0, r - 1);
|
|
215
|
+
brd = tile.get(0, r);
|
|
216
|
+
let minR = Math.min(trd, brd);
|
|
217
|
+
let maxR = Math.max(trd, brd);
|
|
218
|
+
for (c = 1 - buffer; c < tile.width + buffer; c++) {
|
|
219
|
+
tld = trd;
|
|
220
|
+
bld = brd;
|
|
221
|
+
trd = tile.get(c, r - 1);
|
|
222
|
+
brd = tile.get(c, r);
|
|
223
|
+
const minL = minR;
|
|
224
|
+
const maxL = maxR;
|
|
225
|
+
minR = Math.min(trd, brd);
|
|
226
|
+
maxR = Math.max(trd, brd);
|
|
227
|
+
if (isNaN(tld) || isNaN(trd) || isNaN(brd) || isNaN(bld)) {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
const min = Math.min(minL, minR);
|
|
231
|
+
const max2 = Math.max(maxL, maxR);
|
|
232
|
+
const start = Math.ceil(min / interval) * interval;
|
|
233
|
+
const end = Math.floor(max2 / interval) * interval;
|
|
234
|
+
for (let threshold = start; threshold <= end; threshold += interval) {
|
|
235
|
+
const tl = tld > threshold;
|
|
236
|
+
const tr = trd > threshold;
|
|
237
|
+
const bl = bld > threshold;
|
|
238
|
+
const br = brd > threshold;
|
|
239
|
+
for (const segment of CASES[(tl ? 8 : 0) | (tr ? 4 : 0) | (br ? 2 : 0) | (bl ? 1 : 0)]) {
|
|
240
|
+
let fragmentByStart = fragmentByStartByLevel.get(threshold);
|
|
241
|
+
if (!fragmentByStart)
|
|
242
|
+
fragmentByStartByLevel.set(threshold, fragmentByStart = /* @__PURE__ */ new Map());
|
|
243
|
+
let fragmentByEnd = fragmentByEndByLevel.get(threshold);
|
|
244
|
+
if (!fragmentByEnd)
|
|
245
|
+
fragmentByEndByLevel.set(threshold, fragmentByEnd = /* @__PURE__ */ new Map());
|
|
246
|
+
const start2 = segment[0];
|
|
247
|
+
const end2 = segment[1];
|
|
248
|
+
const startIndex = index(tile.width, c, r, start2);
|
|
249
|
+
const endIndex = index(tile.width, c, r, end2);
|
|
250
|
+
let f, g2;
|
|
251
|
+
if (f = fragmentByEnd.get(startIndex)) {
|
|
252
|
+
fragmentByEnd.delete(startIndex);
|
|
253
|
+
if (g2 = fragmentByStart.get(endIndex)) {
|
|
254
|
+
fragmentByStart.delete(endIndex);
|
|
255
|
+
if (f === g2) {
|
|
256
|
+
interpolate(end2, threshold, f.append);
|
|
257
|
+
if (!f.isEmpty()) {
|
|
258
|
+
let list = segments[threshold];
|
|
259
|
+
if (!list) {
|
|
260
|
+
segments[threshold] = list = [];
|
|
261
|
+
}
|
|
262
|
+
list.push(f.lineString());
|
|
263
|
+
}
|
|
264
|
+
} else {
|
|
265
|
+
f.appendFragment(g2);
|
|
266
|
+
fragmentByEnd.set(f.end = g2.end, f);
|
|
267
|
+
}
|
|
268
|
+
} else {
|
|
269
|
+
interpolate(end2, threshold, f.append);
|
|
270
|
+
fragmentByEnd.set(f.end = endIndex, f);
|
|
271
|
+
}
|
|
272
|
+
} else if (f = fragmentByStart.get(endIndex)) {
|
|
273
|
+
fragmentByStart.delete(endIndex);
|
|
274
|
+
interpolate(start2, threshold, f.prepend);
|
|
275
|
+
fragmentByStart.set(f.start = startIndex, f);
|
|
276
|
+
} else {
|
|
277
|
+
const newFrag = new Fragment(startIndex, endIndex);
|
|
278
|
+
interpolate(start2, threshold, newFrag.append);
|
|
279
|
+
interpolate(end2, threshold, newFrag.append);
|
|
280
|
+
fragmentByStart.set(startIndex, newFrag);
|
|
281
|
+
fragmentByEnd.set(endIndex, newFrag);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
for (const [level, fragmentByStart] of fragmentByStartByLevel.entries()) {
|
|
288
|
+
let list = null;
|
|
289
|
+
for (const value of fragmentByStart.values()) {
|
|
290
|
+
if (!value.isEmpty()) {
|
|
291
|
+
if (list == null) {
|
|
292
|
+
list = segments[level] || (segments[level] = []);
|
|
293
|
+
}
|
|
294
|
+
list.push(value.lineString());
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return segments;
|
|
299
|
+
}
|
|
300
|
+
function __rest(s, e) {
|
|
301
|
+
var t = {};
|
|
302
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
303
|
+
t[p] = s[p];
|
|
304
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
305
|
+
for (var i2 = 0, p = Object.getOwnPropertySymbols(s); i2 < p.length; i2++) {
|
|
306
|
+
if (e.indexOf(p[i2]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i2]))
|
|
307
|
+
t[p[i2]] = s[p[i2]];
|
|
308
|
+
}
|
|
309
|
+
return t;
|
|
310
|
+
}
|
|
311
|
+
function __awaiter(thisArg, _arguments, P2, generator) {
|
|
312
|
+
function adopt(value) {
|
|
313
|
+
return value instanceof P2 ? value : new P2(function(resolve) {
|
|
314
|
+
resolve(value);
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
return new (P2 || (P2 = Promise))(function(resolve, reject) {
|
|
318
|
+
function fulfilled(value) {
|
|
319
|
+
try {
|
|
320
|
+
step(generator.next(value));
|
|
321
|
+
} catch (e) {
|
|
322
|
+
reject(e);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
function rejected(value) {
|
|
326
|
+
try {
|
|
327
|
+
step(generator["throw"](value));
|
|
328
|
+
} catch (e) {
|
|
329
|
+
reject(e);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
function step(result) {
|
|
333
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
334
|
+
}
|
|
335
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
function sortedEntries(object) {
|
|
339
|
+
const entries = Object.entries(object);
|
|
340
|
+
entries.sort(([a], [b2]) => a < b2 ? -1 : a > b2 ? 1 : 0);
|
|
341
|
+
return entries;
|
|
342
|
+
}
|
|
343
|
+
function encodeThresholds(thresholds) {
|
|
344
|
+
return sortedEntries(thresholds).map(([key, value]) => [key, ...typeof value === "number" ? [value] : value].join("*")).join("~");
|
|
345
|
+
}
|
|
346
|
+
function decodeThresholds(thresholds) {
|
|
347
|
+
return Object.fromEntries(thresholds.split("~").map((part) => part.split("*").map(Number)).map(([key, ...values]) => [key, values]));
|
|
348
|
+
}
|
|
349
|
+
function encodeOptions(_a2) {
|
|
350
|
+
var { thresholds } = _a2, rest = __rest(_a2, ["thresholds"]);
|
|
351
|
+
return sortedEntries(Object.assign({ thresholds: encodeThresholds(thresholds) }, rest)).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join("&");
|
|
352
|
+
}
|
|
353
|
+
function decodeOptions(options) {
|
|
354
|
+
return Object.fromEntries(options.replace(/^.*\?/, "").split("&").map((part) => {
|
|
355
|
+
const parts = part.split("=").map(decodeURIComponent);
|
|
356
|
+
const k2 = parts[0];
|
|
357
|
+
let v2 = parts[1];
|
|
358
|
+
switch (k2) {
|
|
359
|
+
case "thresholds":
|
|
360
|
+
v2 = decodeThresholds(v2);
|
|
361
|
+
break;
|
|
362
|
+
case "extent":
|
|
363
|
+
case "multiplier":
|
|
364
|
+
case "overzoom":
|
|
365
|
+
case "buffer":
|
|
366
|
+
v2 = Number(v2);
|
|
367
|
+
}
|
|
368
|
+
return [k2, v2];
|
|
369
|
+
}));
|
|
370
|
+
}
|
|
371
|
+
function encodeIndividualOptions(options) {
|
|
372
|
+
return sortedEntries(options).map(([k2, v2]) => `${encodeURIComponent(k2)}=${encodeURIComponent(v2)}`).join(",");
|
|
373
|
+
}
|
|
374
|
+
function getOptionsForZoom(options, zoom) {
|
|
375
|
+
const { thresholds } = options, rest = __rest(options, ["thresholds"]);
|
|
376
|
+
let levels = [];
|
|
377
|
+
let maxLessThanOrEqualTo = -Infinity;
|
|
378
|
+
Object.entries(thresholds).forEach(([zString, value]) => {
|
|
379
|
+
const z = Number(zString);
|
|
380
|
+
if (z <= zoom && z > maxLessThanOrEqualTo) {
|
|
381
|
+
maxLessThanOrEqualTo = z;
|
|
382
|
+
levels = typeof value === "number" ? [value] : value;
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
return Object.assign({ levels }, rest);
|
|
386
|
+
}
|
|
387
|
+
function copy(src) {
|
|
388
|
+
const dst = new ArrayBuffer(src.byteLength);
|
|
389
|
+
new Uint8Array(dst).set(new Uint8Array(src));
|
|
390
|
+
return dst;
|
|
391
|
+
}
|
|
392
|
+
function prepareDemTile(promise, copy2) {
|
|
393
|
+
return promise.then((_a2) => {
|
|
394
|
+
var { data } = _a2, rest = __rest(_a2, ["data"]);
|
|
395
|
+
let newData = data;
|
|
396
|
+
if (copy2) {
|
|
397
|
+
newData = new Float32Array(data.length);
|
|
398
|
+
newData.set(data);
|
|
399
|
+
}
|
|
400
|
+
return Object.assign(Object.assign({}, rest), { data: newData, transferrables: [newData.buffer] });
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
function prepareContourTile(promise) {
|
|
404
|
+
return promise.then(({ arrayBuffer }) => {
|
|
405
|
+
const clone = copy(arrayBuffer);
|
|
406
|
+
return {
|
|
407
|
+
arrayBuffer: clone,
|
|
408
|
+
transferrables: [clone]
|
|
409
|
+
};
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
let supportsOffscreenCanvas = null;
|
|
413
|
+
function offscreenCanvasSupported() {
|
|
414
|
+
if (supportsOffscreenCanvas == null) {
|
|
415
|
+
supportsOffscreenCanvas = typeof OffscreenCanvas !== "undefined" && new OffscreenCanvas(1, 1).getContext("2d") && typeof createImageBitmap === "function";
|
|
416
|
+
}
|
|
417
|
+
return supportsOffscreenCanvas || false;
|
|
418
|
+
}
|
|
419
|
+
let useVideoFrame = null;
|
|
420
|
+
function shouldUseVideoFrame() {
|
|
421
|
+
if (useVideoFrame == null) {
|
|
422
|
+
useVideoFrame = false;
|
|
423
|
+
if (offscreenCanvasSupported() && typeof VideoFrame !== "undefined") {
|
|
424
|
+
const size = 5;
|
|
425
|
+
const canvas2 = new OffscreenCanvas(5, 5);
|
|
426
|
+
const context = canvas2.getContext("2d", { willReadFrequently: true });
|
|
427
|
+
if (context) {
|
|
428
|
+
for (let i2 = 0; i2 < size * size; i2++) {
|
|
429
|
+
const base = i2 * 4;
|
|
430
|
+
context.fillStyle = `rgb(${base},${base + 1},${base + 2})`;
|
|
431
|
+
context.fillRect(i2 % size, Math.floor(i2 / size), 1, 1);
|
|
432
|
+
}
|
|
433
|
+
const data = context.getImageData(0, 0, size, size).data;
|
|
434
|
+
for (let i2 = 0; i2 < size * size * 4; i2++) {
|
|
435
|
+
if (i2 % 4 !== 3 && data[i2] !== i2) {
|
|
436
|
+
useVideoFrame = true;
|
|
437
|
+
break;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return useVideoFrame || false;
|
|
444
|
+
}
|
|
445
|
+
function withTimeout(timeoutMs, value, abortController) {
|
|
446
|
+
let reject = () => {
|
|
447
|
+
};
|
|
448
|
+
const timeout = setTimeout(() => {
|
|
449
|
+
reject(new Error("timed out"));
|
|
450
|
+
abortController === null || abortController === void 0 ? void 0 : abortController.abort();
|
|
451
|
+
}, timeoutMs);
|
|
452
|
+
onAbort(abortController, () => {
|
|
453
|
+
reject(new Error("aborted"));
|
|
454
|
+
clearTimeout(timeout);
|
|
455
|
+
});
|
|
456
|
+
const cancelPromise = new Promise((_2, rej) => {
|
|
457
|
+
reject = rej;
|
|
458
|
+
});
|
|
459
|
+
return Promise.race([
|
|
460
|
+
cancelPromise,
|
|
461
|
+
value.finally(() => clearTimeout(timeout))
|
|
462
|
+
]);
|
|
463
|
+
}
|
|
464
|
+
function onAbort(abortController, action) {
|
|
465
|
+
if (action) {
|
|
466
|
+
abortController === null || abortController === void 0 ? void 0 : abortController.signal.addEventListener("abort", action);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
function isAborted(abortController) {
|
|
470
|
+
var _a2;
|
|
471
|
+
return Boolean((_a2 = abortController === null || abortController === void 0 ? void 0 : abortController.signal) === null || _a2 === void 0 ? void 0 : _a2.aborted);
|
|
472
|
+
}
|
|
473
|
+
let num = 0;
|
|
474
|
+
class AsyncCache {
|
|
475
|
+
constructor(maxSize = 100) {
|
|
476
|
+
this.size = () => this.items.size;
|
|
477
|
+
this.get = (key, supplier, abortController) => {
|
|
478
|
+
let result = this.items.get(key);
|
|
479
|
+
if (!result) {
|
|
480
|
+
const sharedAbortController = new AbortController();
|
|
481
|
+
const value2 = supplier(key, sharedAbortController);
|
|
482
|
+
result = {
|
|
483
|
+
abortController: sharedAbortController,
|
|
484
|
+
item: value2,
|
|
485
|
+
lastUsed: ++num,
|
|
486
|
+
waiting: 1
|
|
487
|
+
};
|
|
488
|
+
this.items.set(key, result);
|
|
489
|
+
this.prune();
|
|
490
|
+
} else {
|
|
491
|
+
result.lastUsed = ++num;
|
|
492
|
+
result.waiting++;
|
|
493
|
+
}
|
|
494
|
+
const items = this.items;
|
|
495
|
+
const value = result.item.then((r) => r, (e) => {
|
|
496
|
+
items.delete(key);
|
|
497
|
+
return Promise.reject(e);
|
|
498
|
+
});
|
|
499
|
+
let canceled = false;
|
|
500
|
+
onAbort(abortController, () => {
|
|
501
|
+
var _a2;
|
|
502
|
+
if (result && result.abortController && !canceled) {
|
|
503
|
+
canceled = true;
|
|
504
|
+
if (--result.waiting <= 0) {
|
|
505
|
+
(_a2 = result.abortController) === null || _a2 === void 0 ? void 0 : _a2.abort();
|
|
506
|
+
items.delete(key);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
});
|
|
510
|
+
return value;
|
|
511
|
+
};
|
|
512
|
+
this.clear = () => this.items.clear();
|
|
513
|
+
this.maxSize = maxSize;
|
|
514
|
+
this.items = /* @__PURE__ */ new Map();
|
|
515
|
+
}
|
|
516
|
+
prune() {
|
|
517
|
+
if (this.items.size > this.maxSize) {
|
|
518
|
+
let minKey;
|
|
519
|
+
let minUse = Infinity;
|
|
520
|
+
this.items.forEach((value, key) => {
|
|
521
|
+
if (value.lastUsed < minUse) {
|
|
522
|
+
minUse = value.lastUsed;
|
|
523
|
+
minKey = key;
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
if (typeof minKey !== "undefined") {
|
|
527
|
+
this.items.delete(minKey);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
let offscreenCanvas;
|
|
533
|
+
let offscreenContext;
|
|
534
|
+
let canvas;
|
|
535
|
+
let canvasContext;
|
|
536
|
+
function decodeImageModern(blob, encoding, abortController) {
|
|
537
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
538
|
+
const img = yield createImageBitmap(blob);
|
|
539
|
+
if (isAborted(abortController))
|
|
540
|
+
return null;
|
|
541
|
+
return decodeImageUsingOffscreenCanvas(img, encoding);
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
function decodeImageUsingOffscreenCanvas(img, encoding) {
|
|
545
|
+
if (!offscreenCanvas) {
|
|
546
|
+
offscreenCanvas = new OffscreenCanvas(img.width, img.height);
|
|
547
|
+
offscreenContext = offscreenCanvas.getContext("2d", {
|
|
548
|
+
willReadFrequently: true
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
return getElevations(img, encoding, offscreenCanvas, offscreenContext);
|
|
552
|
+
}
|
|
553
|
+
function decodeImageVideoFrame(blob, encoding, abortController) {
|
|
554
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
555
|
+
var _a2, _b2, _c;
|
|
556
|
+
const img = yield createImageBitmap(blob);
|
|
557
|
+
if (isAborted(abortController))
|
|
558
|
+
return null;
|
|
559
|
+
const vf = new VideoFrame(img, { timestamp: 0 });
|
|
560
|
+
try {
|
|
561
|
+
const valid = ((_a2 = vf === null || vf === void 0 ? void 0 : vf.format) === null || _a2 === void 0 ? void 0 : _a2.startsWith("BGR")) || ((_b2 = vf === null || vf === void 0 ? void 0 : vf.format) === null || _b2 === void 0 ? void 0 : _b2.startsWith("RGB"));
|
|
562
|
+
if (!valid) {
|
|
563
|
+
throw new Error(`Unrecognized format: ${vf === null || vf === void 0 ? void 0 : vf.format}`);
|
|
564
|
+
}
|
|
565
|
+
const swapBR = (_c = vf === null || vf === void 0 ? void 0 : vf.format) === null || _c === void 0 ? void 0 : _c.startsWith("BGR");
|
|
566
|
+
const size = vf.allocationSize();
|
|
567
|
+
const data = new Uint8ClampedArray(size);
|
|
568
|
+
yield vf.copyTo(data);
|
|
569
|
+
if (swapBR) {
|
|
570
|
+
for (let i2 = 0; i2 < data.length; i2 += 4) {
|
|
571
|
+
const tmp = data[i2];
|
|
572
|
+
data[i2] = data[i2 + 2];
|
|
573
|
+
data[i2 + 2] = tmp;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
return decodeParsedImage(img.width, img.height, encoding, data);
|
|
577
|
+
} catch (_2) {
|
|
578
|
+
if (isAborted(abortController))
|
|
579
|
+
return null;
|
|
580
|
+
return decodeImageUsingOffscreenCanvas(img, encoding);
|
|
581
|
+
} finally {
|
|
582
|
+
vf.close();
|
|
583
|
+
}
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
function decodeImageOld(blob, encoding, abortController) {
|
|
587
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
588
|
+
if (!canvas) {
|
|
589
|
+
canvas = document.createElement("canvas");
|
|
590
|
+
canvasContext = canvas.getContext("2d", {
|
|
591
|
+
willReadFrequently: true
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
const img = new Image();
|
|
595
|
+
onAbort(abortController, () => img.src = "");
|
|
596
|
+
const fetchedImage = yield new Promise((resolve, reject) => {
|
|
597
|
+
img.onload = () => {
|
|
598
|
+
if (!isAborted(abortController))
|
|
599
|
+
resolve(img);
|
|
600
|
+
URL.revokeObjectURL(img.src);
|
|
601
|
+
img.onload = null;
|
|
602
|
+
};
|
|
603
|
+
img.onerror = () => reject(new Error("Could not load image."));
|
|
604
|
+
img.src = blob.size ? URL.createObjectURL(blob) : "";
|
|
605
|
+
});
|
|
606
|
+
return getElevations(fetchedImage, encoding, canvas, canvasContext);
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
function decodeImageOnMainThread(blob, encoding, abortController) {
|
|
610
|
+
return self.actor.send("decodeImage", [], abortController, void 0, blob, encoding);
|
|
611
|
+
}
|
|
612
|
+
function isWorker() {
|
|
613
|
+
return (
|
|
614
|
+
// @ts-expect-error WorkerGlobalScope defined
|
|
615
|
+
typeof WorkerGlobalScope !== "undefined" && typeof self !== "undefined" && // @ts-expect-error WorkerGlobalScope defined
|
|
616
|
+
self instanceof WorkerGlobalScope
|
|
617
|
+
);
|
|
618
|
+
}
|
|
619
|
+
const defaultDecoder = shouldUseVideoFrame() ? decodeImageVideoFrame : offscreenCanvasSupported() ? decodeImageModern : isWorker() ? decodeImageOnMainThread : decodeImageOld;
|
|
620
|
+
function getElevations(img, encoding, canvas2, canvasContext2) {
|
|
621
|
+
canvas2.width = img.width;
|
|
622
|
+
canvas2.height = img.height;
|
|
623
|
+
if (!canvasContext2)
|
|
624
|
+
throw new Error("failed to get context");
|
|
625
|
+
canvasContext2.drawImage(img, 0, 0, img.width, img.height);
|
|
626
|
+
const rgba = canvasContext2.getImageData(0, 0, img.width, img.height).data;
|
|
627
|
+
return decodeParsedImage(img.width, img.height, encoding, rgba);
|
|
628
|
+
}
|
|
629
|
+
function decodeParsedImage(width, height, encoding, input) {
|
|
630
|
+
const decoder = encoding === "mapbox" ? (r, g2, b2) => -1e4 + (r * 256 * 256 + g2 * 256 + b2) * 0.1 : (r, g2, b2) => r * 256 + g2 + b2 / 256 - 32768;
|
|
631
|
+
const data = new Float32Array(width * height);
|
|
632
|
+
for (let i2 = 0; i2 < input.length; i2 += 4) {
|
|
633
|
+
data[i2 / 4] = decoder(input[i2], input[i2 + 1], input[i2 + 2]);
|
|
634
|
+
}
|
|
635
|
+
return { width, height, data };
|
|
636
|
+
}
|
|
637
|
+
const MIN_VALID_M = -12e3;
|
|
638
|
+
const MAX_VALID_M = 9e3;
|
|
639
|
+
function defaultIsValid(number) {
|
|
640
|
+
return !isNaN(number) && number >= MIN_VALID_M && number <= MAX_VALID_M;
|
|
641
|
+
}
|
|
642
|
+
class HeightTile {
|
|
643
|
+
constructor(width, height, get) {
|
|
644
|
+
this.split = (subz, subx, suby) => {
|
|
645
|
+
if (subz === 0)
|
|
646
|
+
return this;
|
|
647
|
+
const by = 1 << subz;
|
|
648
|
+
const dx = subx * this.width / by;
|
|
649
|
+
const dy = suby * this.height / by;
|
|
650
|
+
return new HeightTile(this.width / by, this.height / by, (x3, y) => this.get(x3 + dx, y + dy));
|
|
651
|
+
};
|
|
652
|
+
this.subsamplePixelCenters = (factor) => {
|
|
653
|
+
const lerp = (a, b2, f) => isNaN(a) ? b2 : isNaN(b2) ? a : a + (b2 - a) * f;
|
|
654
|
+
if (factor <= 1)
|
|
655
|
+
return this;
|
|
656
|
+
const sub = 0.5 - 1 / (2 * factor);
|
|
657
|
+
const blerper = (x3, y) => {
|
|
658
|
+
const dx = x3 / factor - sub;
|
|
659
|
+
const dy = y / factor - sub;
|
|
660
|
+
const ox = Math.floor(dx);
|
|
661
|
+
const oy = Math.floor(dy);
|
|
662
|
+
const a = this.get(ox, oy);
|
|
663
|
+
const b2 = this.get(ox + 1, oy);
|
|
664
|
+
const c = this.get(ox, oy + 1);
|
|
665
|
+
const d = this.get(ox + 1, oy + 1);
|
|
666
|
+
const fx = dx - ox;
|
|
667
|
+
const fy = dy - oy;
|
|
668
|
+
const top = lerp(a, b2, fx);
|
|
669
|
+
const bottom = lerp(c, d, fx);
|
|
670
|
+
return lerp(top, bottom, fy);
|
|
671
|
+
};
|
|
672
|
+
return new HeightTile(this.width * factor, this.height * factor, blerper);
|
|
673
|
+
};
|
|
674
|
+
this.averagePixelCentersToGrid = (radius = 1) => new HeightTile(this.width + 1, this.height + 1, (x3, y) => {
|
|
675
|
+
let sum = 0, count = 0, v2 = 0;
|
|
676
|
+
for (let newX = x3 - radius; newX < x3 + radius; newX++) {
|
|
677
|
+
for (let newY = y - radius; newY < y + radius; newY++) {
|
|
678
|
+
if (!isNaN(v2 = this.get(newX, newY))) {
|
|
679
|
+
count++;
|
|
680
|
+
sum += v2;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
return count === 0 ? NaN : sum / count;
|
|
685
|
+
});
|
|
686
|
+
this.scaleElevation = (multiplier) => multiplier === 1 ? this : new HeightTile(this.width, this.height, (x3, y) => this.get(x3, y) * multiplier);
|
|
687
|
+
this.materialize = (buffer = 2) => {
|
|
688
|
+
const stride = this.width + 2 * buffer;
|
|
689
|
+
const data = new Float32Array(stride * (this.height + 2 * buffer));
|
|
690
|
+
let idx = 0;
|
|
691
|
+
for (let y = -buffer; y < this.height + buffer; y++) {
|
|
692
|
+
for (let x3 = -buffer; x3 < this.width + buffer; x3++) {
|
|
693
|
+
data[idx++] = this.get(x3, y);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return new HeightTile(this.width, this.height, (x3, y) => data[(y + buffer) * stride + x3 + buffer]);
|
|
697
|
+
};
|
|
698
|
+
this.get = get;
|
|
699
|
+
this.width = width;
|
|
700
|
+
this.height = height;
|
|
701
|
+
}
|
|
702
|
+
/** Construct a height tile from raw DEM pixel values */
|
|
703
|
+
static fromRawDem(demTile) {
|
|
704
|
+
return new HeightTile(demTile.width, demTile.height, (x3, y) => {
|
|
705
|
+
const value = demTile.data[y * demTile.width + x3];
|
|
706
|
+
return defaultIsValid(value) ? value : NaN;
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* Construct a height tile from a DEM tile plus it's 8 neighbors, so that
|
|
711
|
+
* you can request `x` or `y` outside the bounds of the original tile.
|
|
712
|
+
*
|
|
713
|
+
* @param neighbors An array containing tiles: `[nw, n, ne, w, c, e, sw, s, se]`
|
|
714
|
+
*/
|
|
715
|
+
static combineNeighbors(neighbors) {
|
|
716
|
+
if (neighbors.length !== 9) {
|
|
717
|
+
throw new Error("Must include a tile plus 8 neighbors");
|
|
718
|
+
}
|
|
719
|
+
const mainTile = neighbors[4];
|
|
720
|
+
if (!mainTile) {
|
|
721
|
+
return void 0;
|
|
722
|
+
}
|
|
723
|
+
const width = mainTile.width;
|
|
724
|
+
const height = mainTile.height;
|
|
725
|
+
return new HeightTile(width, height, (x3, y) => {
|
|
726
|
+
let gridIdx = 0;
|
|
727
|
+
if (y < 0) {
|
|
728
|
+
y += height;
|
|
729
|
+
} else if (y < height) {
|
|
730
|
+
gridIdx += 3;
|
|
731
|
+
} else {
|
|
732
|
+
y -= height;
|
|
733
|
+
gridIdx += 6;
|
|
734
|
+
}
|
|
735
|
+
if (x3 < 0) {
|
|
736
|
+
x3 += width;
|
|
737
|
+
} else if (x3 < width) {
|
|
738
|
+
gridIdx += 1;
|
|
739
|
+
} else {
|
|
740
|
+
x3 -= width;
|
|
741
|
+
gridIdx += 2;
|
|
742
|
+
}
|
|
743
|
+
const grid = neighbors[gridIdx];
|
|
744
|
+
return grid ? grid.get(x3, y) : NaN;
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
const SHIFT_LEFT_32 = (1 << 16) * (1 << 16);
|
|
749
|
+
const SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;
|
|
750
|
+
const TEXT_DECODER_MIN_LENGTH = 12;
|
|
751
|
+
const utf8TextDecoder = typeof TextDecoder === "undefined" ? null : new TextDecoder("utf-8");
|
|
752
|
+
const PBF_VARINT = 0;
|
|
753
|
+
const PBF_FIXED64 = 1;
|
|
754
|
+
const PBF_BYTES = 2;
|
|
755
|
+
const PBF_FIXED32 = 5;
|
|
756
|
+
class Pbf {
|
|
757
|
+
/**
|
|
758
|
+
* @param {Uint8Array | ArrayBuffer} [buf]
|
|
759
|
+
*/
|
|
760
|
+
constructor(buf = new Uint8Array(16)) {
|
|
761
|
+
this.buf = ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf);
|
|
762
|
+
this.dataView = new DataView(this.buf.buffer);
|
|
763
|
+
this.pos = 0;
|
|
764
|
+
this.type = 0;
|
|
765
|
+
this.length = this.buf.length;
|
|
766
|
+
}
|
|
767
|
+
// === READING =================================================================
|
|
768
|
+
/**
|
|
769
|
+
* @template T
|
|
770
|
+
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
|
|
771
|
+
* @param {T} result
|
|
772
|
+
* @param {number} [end]
|
|
773
|
+
*/
|
|
774
|
+
readFields(readField, result, end = this.length) {
|
|
775
|
+
while (this.pos < end) {
|
|
776
|
+
const val = this.readVarint(), tag = val >> 3, startPos = this.pos;
|
|
777
|
+
this.type = val & 7;
|
|
778
|
+
readField(tag, result, this);
|
|
779
|
+
if (this.pos === startPos) this.skip(val);
|
|
780
|
+
}
|
|
781
|
+
return result;
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* @template T
|
|
785
|
+
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
|
|
786
|
+
* @param {T} result
|
|
787
|
+
*/
|
|
788
|
+
readMessage(readField, result) {
|
|
789
|
+
return this.readFields(readField, result, this.readVarint() + this.pos);
|
|
790
|
+
}
|
|
791
|
+
readFixed32() {
|
|
792
|
+
const val = this.dataView.getUint32(this.pos, true);
|
|
793
|
+
this.pos += 4;
|
|
794
|
+
return val;
|
|
795
|
+
}
|
|
796
|
+
readSFixed32() {
|
|
797
|
+
const val = this.dataView.getInt32(this.pos, true);
|
|
798
|
+
this.pos += 4;
|
|
799
|
+
return val;
|
|
800
|
+
}
|
|
801
|
+
// 64-bit int handling is based on github.com/dpw/node-buffer-more-ints (MIT-licensed)
|
|
802
|
+
readFixed64() {
|
|
803
|
+
const val = this.dataView.getUint32(this.pos, true) + this.dataView.getUint32(this.pos + 4, true) * SHIFT_LEFT_32;
|
|
804
|
+
this.pos += 8;
|
|
805
|
+
return val;
|
|
806
|
+
}
|
|
807
|
+
readSFixed64() {
|
|
808
|
+
const val = this.dataView.getUint32(this.pos, true) + this.dataView.getInt32(this.pos + 4, true) * SHIFT_LEFT_32;
|
|
809
|
+
this.pos += 8;
|
|
810
|
+
return val;
|
|
811
|
+
}
|
|
812
|
+
readFloat() {
|
|
813
|
+
const val = this.dataView.getFloat32(this.pos, true);
|
|
814
|
+
this.pos += 4;
|
|
815
|
+
return val;
|
|
816
|
+
}
|
|
817
|
+
readDouble() {
|
|
818
|
+
const val = this.dataView.getFloat64(this.pos, true);
|
|
819
|
+
this.pos += 8;
|
|
820
|
+
return val;
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* @param {boolean} [isSigned]
|
|
824
|
+
*/
|
|
825
|
+
readVarint(isSigned) {
|
|
826
|
+
const buf = this.buf;
|
|
827
|
+
let val, b2;
|
|
828
|
+
b2 = buf[this.pos++];
|
|
829
|
+
val = b2 & 127;
|
|
830
|
+
if (b2 < 128) return val;
|
|
831
|
+
b2 = buf[this.pos++];
|
|
832
|
+
val |= (b2 & 127) << 7;
|
|
833
|
+
if (b2 < 128) return val;
|
|
834
|
+
b2 = buf[this.pos++];
|
|
835
|
+
val |= (b2 & 127) << 14;
|
|
836
|
+
if (b2 < 128) return val;
|
|
837
|
+
b2 = buf[this.pos++];
|
|
838
|
+
val |= (b2 & 127) << 21;
|
|
839
|
+
if (b2 < 128) return val;
|
|
840
|
+
b2 = buf[this.pos];
|
|
841
|
+
val |= (b2 & 15) << 28;
|
|
842
|
+
return readVarintRemainder(val, isSigned, this);
|
|
843
|
+
}
|
|
844
|
+
readVarint64() {
|
|
845
|
+
return this.readVarint(true);
|
|
846
|
+
}
|
|
847
|
+
readSVarint() {
|
|
848
|
+
const num2 = this.readVarint();
|
|
849
|
+
return num2 % 2 === 1 ? (num2 + 1) / -2 : num2 / 2;
|
|
850
|
+
}
|
|
851
|
+
readBoolean() {
|
|
852
|
+
return Boolean(this.readVarint());
|
|
853
|
+
}
|
|
854
|
+
readString() {
|
|
855
|
+
const end = this.readVarint() + this.pos;
|
|
856
|
+
const pos = this.pos;
|
|
857
|
+
this.pos = end;
|
|
858
|
+
if (end - pos >= TEXT_DECODER_MIN_LENGTH && utf8TextDecoder) {
|
|
859
|
+
return utf8TextDecoder.decode(this.buf.subarray(pos, end));
|
|
860
|
+
}
|
|
861
|
+
return readUtf8(this.buf, pos, end);
|
|
862
|
+
}
|
|
863
|
+
readBytes() {
|
|
864
|
+
const end = this.readVarint() + this.pos, buffer = this.buf.subarray(this.pos, end);
|
|
865
|
+
this.pos = end;
|
|
866
|
+
return buffer;
|
|
867
|
+
}
|
|
868
|
+
// verbose for performance reasons; doesn't affect gzipped size
|
|
869
|
+
/**
|
|
870
|
+
* @param {number[]} [arr]
|
|
871
|
+
* @param {boolean} [isSigned]
|
|
872
|
+
*/
|
|
873
|
+
readPackedVarint(arr = [], isSigned) {
|
|
874
|
+
const end = this.readPackedEnd();
|
|
875
|
+
while (this.pos < end) arr.push(this.readVarint(isSigned));
|
|
876
|
+
return arr;
|
|
877
|
+
}
|
|
878
|
+
/** @param {number[]} [arr] */
|
|
879
|
+
readPackedSVarint(arr = []) {
|
|
880
|
+
const end = this.readPackedEnd();
|
|
881
|
+
while (this.pos < end) arr.push(this.readSVarint());
|
|
882
|
+
return arr;
|
|
883
|
+
}
|
|
884
|
+
/** @param {boolean[]} [arr] */
|
|
885
|
+
readPackedBoolean(arr = []) {
|
|
886
|
+
const end = this.readPackedEnd();
|
|
887
|
+
while (this.pos < end) arr.push(this.readBoolean());
|
|
888
|
+
return arr;
|
|
889
|
+
}
|
|
890
|
+
/** @param {number[]} [arr] */
|
|
891
|
+
readPackedFloat(arr = []) {
|
|
892
|
+
const end = this.readPackedEnd();
|
|
893
|
+
while (this.pos < end) arr.push(this.readFloat());
|
|
894
|
+
return arr;
|
|
895
|
+
}
|
|
896
|
+
/** @param {number[]} [arr] */
|
|
897
|
+
readPackedDouble(arr = []) {
|
|
898
|
+
const end = this.readPackedEnd();
|
|
899
|
+
while (this.pos < end) arr.push(this.readDouble());
|
|
900
|
+
return arr;
|
|
901
|
+
}
|
|
902
|
+
/** @param {number[]} [arr] */
|
|
903
|
+
readPackedFixed32(arr = []) {
|
|
904
|
+
const end = this.readPackedEnd();
|
|
905
|
+
while (this.pos < end) arr.push(this.readFixed32());
|
|
906
|
+
return arr;
|
|
907
|
+
}
|
|
908
|
+
/** @param {number[]} [arr] */
|
|
909
|
+
readPackedSFixed32(arr = []) {
|
|
910
|
+
const end = this.readPackedEnd();
|
|
911
|
+
while (this.pos < end) arr.push(this.readSFixed32());
|
|
912
|
+
return arr;
|
|
913
|
+
}
|
|
914
|
+
/** @param {number[]} [arr] */
|
|
915
|
+
readPackedFixed64(arr = []) {
|
|
916
|
+
const end = this.readPackedEnd();
|
|
917
|
+
while (this.pos < end) arr.push(this.readFixed64());
|
|
918
|
+
return arr;
|
|
919
|
+
}
|
|
920
|
+
/** @param {number[]} [arr] */
|
|
921
|
+
readPackedSFixed64(arr = []) {
|
|
922
|
+
const end = this.readPackedEnd();
|
|
923
|
+
while (this.pos < end) arr.push(this.readSFixed64());
|
|
924
|
+
return arr;
|
|
925
|
+
}
|
|
926
|
+
readPackedEnd() {
|
|
927
|
+
return this.type === PBF_BYTES ? this.readVarint() + this.pos : this.pos + 1;
|
|
928
|
+
}
|
|
929
|
+
/** @param {number} val */
|
|
930
|
+
skip(val) {
|
|
931
|
+
const type = val & 7;
|
|
932
|
+
if (type === PBF_VARINT) while (this.buf[this.pos++] > 127) {
|
|
933
|
+
}
|
|
934
|
+
else if (type === PBF_BYTES) this.pos = this.readVarint() + this.pos;
|
|
935
|
+
else if (type === PBF_FIXED32) this.pos += 4;
|
|
936
|
+
else if (type === PBF_FIXED64) this.pos += 8;
|
|
937
|
+
else throw new Error(`Unimplemented type: ${type}`);
|
|
938
|
+
}
|
|
939
|
+
// === WRITING =================================================================
|
|
940
|
+
/**
|
|
941
|
+
* @param {number} tag
|
|
942
|
+
* @param {number} type
|
|
943
|
+
*/
|
|
944
|
+
writeTag(tag, type) {
|
|
945
|
+
this.writeVarint(tag << 3 | type);
|
|
946
|
+
}
|
|
947
|
+
/** @param {number} min */
|
|
948
|
+
realloc(min) {
|
|
949
|
+
let length = this.length || 16;
|
|
950
|
+
while (length < this.pos + min) length *= 2;
|
|
951
|
+
if (length !== this.length) {
|
|
952
|
+
const buf = new Uint8Array(length);
|
|
953
|
+
buf.set(this.buf);
|
|
954
|
+
this.buf = buf;
|
|
955
|
+
this.dataView = new DataView(buf.buffer);
|
|
956
|
+
this.length = length;
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
finish() {
|
|
960
|
+
this.length = this.pos;
|
|
961
|
+
this.pos = 0;
|
|
962
|
+
return this.buf.subarray(0, this.length);
|
|
963
|
+
}
|
|
964
|
+
/** @param {number} val */
|
|
965
|
+
writeFixed32(val) {
|
|
966
|
+
this.realloc(4);
|
|
967
|
+
this.dataView.setInt32(this.pos, val, true);
|
|
968
|
+
this.pos += 4;
|
|
969
|
+
}
|
|
970
|
+
/** @param {number} val */
|
|
971
|
+
writeSFixed32(val) {
|
|
972
|
+
this.realloc(4);
|
|
973
|
+
this.dataView.setInt32(this.pos, val, true);
|
|
974
|
+
this.pos += 4;
|
|
975
|
+
}
|
|
976
|
+
/** @param {number} val */
|
|
977
|
+
writeFixed64(val) {
|
|
978
|
+
this.realloc(8);
|
|
979
|
+
this.dataView.setInt32(this.pos, val & -1, true);
|
|
980
|
+
this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
|
|
981
|
+
this.pos += 8;
|
|
982
|
+
}
|
|
983
|
+
/** @param {number} val */
|
|
984
|
+
writeSFixed64(val) {
|
|
985
|
+
this.realloc(8);
|
|
986
|
+
this.dataView.setInt32(this.pos, val & -1, true);
|
|
987
|
+
this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
|
|
988
|
+
this.pos += 8;
|
|
989
|
+
}
|
|
990
|
+
/** @param {number} val */
|
|
991
|
+
writeVarint(val) {
|
|
992
|
+
val = +val || 0;
|
|
993
|
+
if (val > 268435455 || val < 0) {
|
|
994
|
+
writeBigVarint(val, this);
|
|
995
|
+
return;
|
|
996
|
+
}
|
|
997
|
+
this.realloc(4);
|
|
998
|
+
this.buf[this.pos++] = val & 127 | (val > 127 ? 128 : 0);
|
|
999
|
+
if (val <= 127) return;
|
|
1000
|
+
this.buf[this.pos++] = (val >>>= 7) & 127 | (val > 127 ? 128 : 0);
|
|
1001
|
+
if (val <= 127) return;
|
|
1002
|
+
this.buf[this.pos++] = (val >>>= 7) & 127 | (val > 127 ? 128 : 0);
|
|
1003
|
+
if (val <= 127) return;
|
|
1004
|
+
this.buf[this.pos++] = val >>> 7 & 127;
|
|
1005
|
+
}
|
|
1006
|
+
/** @param {number} val */
|
|
1007
|
+
writeSVarint(val) {
|
|
1008
|
+
this.writeVarint(val < 0 ? -val * 2 - 1 : val * 2);
|
|
1009
|
+
}
|
|
1010
|
+
/** @param {boolean} val */
|
|
1011
|
+
writeBoolean(val) {
|
|
1012
|
+
this.writeVarint(+val);
|
|
1013
|
+
}
|
|
1014
|
+
/** @param {string} str */
|
|
1015
|
+
writeString(str) {
|
|
1016
|
+
str = String(str);
|
|
1017
|
+
this.realloc(str.length * 4);
|
|
1018
|
+
this.pos++;
|
|
1019
|
+
const startPos = this.pos;
|
|
1020
|
+
this.pos = writeUtf8(this.buf, str, this.pos);
|
|
1021
|
+
const len = this.pos - startPos;
|
|
1022
|
+
if (len >= 128) makeRoomForExtraLength(startPos, len, this);
|
|
1023
|
+
this.pos = startPos - 1;
|
|
1024
|
+
this.writeVarint(len);
|
|
1025
|
+
this.pos += len;
|
|
1026
|
+
}
|
|
1027
|
+
/** @param {number} val */
|
|
1028
|
+
writeFloat(val) {
|
|
1029
|
+
this.realloc(4);
|
|
1030
|
+
this.dataView.setFloat32(this.pos, val, true);
|
|
1031
|
+
this.pos += 4;
|
|
1032
|
+
}
|
|
1033
|
+
/** @param {number} val */
|
|
1034
|
+
writeDouble(val) {
|
|
1035
|
+
this.realloc(8);
|
|
1036
|
+
this.dataView.setFloat64(this.pos, val, true);
|
|
1037
|
+
this.pos += 8;
|
|
1038
|
+
}
|
|
1039
|
+
/** @param {Uint8Array} buffer */
|
|
1040
|
+
writeBytes(buffer) {
|
|
1041
|
+
const len = buffer.length;
|
|
1042
|
+
this.writeVarint(len);
|
|
1043
|
+
this.realloc(len);
|
|
1044
|
+
for (let i2 = 0; i2 < len; i2++) this.buf[this.pos++] = buffer[i2];
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* @template T
|
|
1048
|
+
* @param {(obj: T, pbf: Pbf) => void} fn
|
|
1049
|
+
* @param {T} obj
|
|
1050
|
+
*/
|
|
1051
|
+
writeRawMessage(fn, obj) {
|
|
1052
|
+
this.pos++;
|
|
1053
|
+
const startPos = this.pos;
|
|
1054
|
+
fn(obj, this);
|
|
1055
|
+
const len = this.pos - startPos;
|
|
1056
|
+
if (len >= 128) makeRoomForExtraLength(startPos, len, this);
|
|
1057
|
+
this.pos = startPos - 1;
|
|
1058
|
+
this.writeVarint(len);
|
|
1059
|
+
this.pos += len;
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* @template T
|
|
1063
|
+
* @param {number} tag
|
|
1064
|
+
* @param {(obj: T, pbf: Pbf) => void} fn
|
|
1065
|
+
* @param {T} obj
|
|
1066
|
+
*/
|
|
1067
|
+
writeMessage(tag, fn, obj) {
|
|
1068
|
+
this.writeTag(tag, PBF_BYTES);
|
|
1069
|
+
this.writeRawMessage(fn, obj);
|
|
1070
|
+
}
|
|
1071
|
+
/**
|
|
1072
|
+
* @param {number} tag
|
|
1073
|
+
* @param {number[]} arr
|
|
1074
|
+
*/
|
|
1075
|
+
writePackedVarint(tag, arr) {
|
|
1076
|
+
if (arr.length) this.writeMessage(tag, writePackedVarint, arr);
|
|
1077
|
+
}
|
|
1078
|
+
/**
|
|
1079
|
+
* @param {number} tag
|
|
1080
|
+
* @param {number[]} arr
|
|
1081
|
+
*/
|
|
1082
|
+
writePackedSVarint(tag, arr) {
|
|
1083
|
+
if (arr.length) this.writeMessage(tag, writePackedSVarint, arr);
|
|
1084
|
+
}
|
|
1085
|
+
/**
|
|
1086
|
+
* @param {number} tag
|
|
1087
|
+
* @param {boolean[]} arr
|
|
1088
|
+
*/
|
|
1089
|
+
writePackedBoolean(tag, arr) {
|
|
1090
|
+
if (arr.length) this.writeMessage(tag, writePackedBoolean, arr);
|
|
1091
|
+
}
|
|
1092
|
+
/**
|
|
1093
|
+
* @param {number} tag
|
|
1094
|
+
* @param {number[]} arr
|
|
1095
|
+
*/
|
|
1096
|
+
writePackedFloat(tag, arr) {
|
|
1097
|
+
if (arr.length) this.writeMessage(tag, writePackedFloat, arr);
|
|
1098
|
+
}
|
|
1099
|
+
/**
|
|
1100
|
+
* @param {number} tag
|
|
1101
|
+
* @param {number[]} arr
|
|
1102
|
+
*/
|
|
1103
|
+
writePackedDouble(tag, arr) {
|
|
1104
|
+
if (arr.length) this.writeMessage(tag, writePackedDouble, arr);
|
|
1105
|
+
}
|
|
1106
|
+
/**
|
|
1107
|
+
* @param {number} tag
|
|
1108
|
+
* @param {number[]} arr
|
|
1109
|
+
*/
|
|
1110
|
+
writePackedFixed32(tag, arr) {
|
|
1111
|
+
if (arr.length) this.writeMessage(tag, writePackedFixed32, arr);
|
|
1112
|
+
}
|
|
1113
|
+
/**
|
|
1114
|
+
* @param {number} tag
|
|
1115
|
+
* @param {number[]} arr
|
|
1116
|
+
*/
|
|
1117
|
+
writePackedSFixed32(tag, arr) {
|
|
1118
|
+
if (arr.length) this.writeMessage(tag, writePackedSFixed32, arr);
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* @param {number} tag
|
|
1122
|
+
* @param {number[]} arr
|
|
1123
|
+
*/
|
|
1124
|
+
writePackedFixed64(tag, arr) {
|
|
1125
|
+
if (arr.length) this.writeMessage(tag, writePackedFixed64, arr);
|
|
1126
|
+
}
|
|
1127
|
+
/**
|
|
1128
|
+
* @param {number} tag
|
|
1129
|
+
* @param {number[]} arr
|
|
1130
|
+
*/
|
|
1131
|
+
writePackedSFixed64(tag, arr) {
|
|
1132
|
+
if (arr.length) this.writeMessage(tag, writePackedSFixed64, arr);
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* @param {number} tag
|
|
1136
|
+
* @param {Uint8Array} buffer
|
|
1137
|
+
*/
|
|
1138
|
+
writeBytesField(tag, buffer) {
|
|
1139
|
+
this.writeTag(tag, PBF_BYTES);
|
|
1140
|
+
this.writeBytes(buffer);
|
|
1141
|
+
}
|
|
1142
|
+
/**
|
|
1143
|
+
* @param {number} tag
|
|
1144
|
+
* @param {number} val
|
|
1145
|
+
*/
|
|
1146
|
+
writeFixed32Field(tag, val) {
|
|
1147
|
+
this.writeTag(tag, PBF_FIXED32);
|
|
1148
|
+
this.writeFixed32(val);
|
|
1149
|
+
}
|
|
1150
|
+
/**
|
|
1151
|
+
* @param {number} tag
|
|
1152
|
+
* @param {number} val
|
|
1153
|
+
*/
|
|
1154
|
+
writeSFixed32Field(tag, val) {
|
|
1155
|
+
this.writeTag(tag, PBF_FIXED32);
|
|
1156
|
+
this.writeSFixed32(val);
|
|
1157
|
+
}
|
|
1158
|
+
/**
|
|
1159
|
+
* @param {number} tag
|
|
1160
|
+
* @param {number} val
|
|
1161
|
+
*/
|
|
1162
|
+
writeFixed64Field(tag, val) {
|
|
1163
|
+
this.writeTag(tag, PBF_FIXED64);
|
|
1164
|
+
this.writeFixed64(val);
|
|
1165
|
+
}
|
|
1166
|
+
/**
|
|
1167
|
+
* @param {number} tag
|
|
1168
|
+
* @param {number} val
|
|
1169
|
+
*/
|
|
1170
|
+
writeSFixed64Field(tag, val) {
|
|
1171
|
+
this.writeTag(tag, PBF_FIXED64);
|
|
1172
|
+
this.writeSFixed64(val);
|
|
1173
|
+
}
|
|
1174
|
+
/**
|
|
1175
|
+
* @param {number} tag
|
|
1176
|
+
* @param {number} val
|
|
1177
|
+
*/
|
|
1178
|
+
writeVarintField(tag, val) {
|
|
1179
|
+
this.writeTag(tag, PBF_VARINT);
|
|
1180
|
+
this.writeVarint(val);
|
|
1181
|
+
}
|
|
1182
|
+
/**
|
|
1183
|
+
* @param {number} tag
|
|
1184
|
+
* @param {number} val
|
|
1185
|
+
*/
|
|
1186
|
+
writeSVarintField(tag, val) {
|
|
1187
|
+
this.writeTag(tag, PBF_VARINT);
|
|
1188
|
+
this.writeSVarint(val);
|
|
1189
|
+
}
|
|
1190
|
+
/**
|
|
1191
|
+
* @param {number} tag
|
|
1192
|
+
* @param {string} str
|
|
1193
|
+
*/
|
|
1194
|
+
writeStringField(tag, str) {
|
|
1195
|
+
this.writeTag(tag, PBF_BYTES);
|
|
1196
|
+
this.writeString(str);
|
|
1197
|
+
}
|
|
1198
|
+
/**
|
|
1199
|
+
* @param {number} tag
|
|
1200
|
+
* @param {number} val
|
|
1201
|
+
*/
|
|
1202
|
+
writeFloatField(tag, val) {
|
|
1203
|
+
this.writeTag(tag, PBF_FIXED32);
|
|
1204
|
+
this.writeFloat(val);
|
|
1205
|
+
}
|
|
1206
|
+
/**
|
|
1207
|
+
* @param {number} tag
|
|
1208
|
+
* @param {number} val
|
|
1209
|
+
*/
|
|
1210
|
+
writeDoubleField(tag, val) {
|
|
1211
|
+
this.writeTag(tag, PBF_FIXED64);
|
|
1212
|
+
this.writeDouble(val);
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* @param {number} tag
|
|
1216
|
+
* @param {boolean} val
|
|
1217
|
+
*/
|
|
1218
|
+
writeBooleanField(tag, val) {
|
|
1219
|
+
this.writeVarintField(tag, +val);
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
function readVarintRemainder(l2, s, p) {
|
|
1223
|
+
const buf = p.buf;
|
|
1224
|
+
let h, b2;
|
|
1225
|
+
b2 = buf[p.pos++];
|
|
1226
|
+
h = (b2 & 112) >> 4;
|
|
1227
|
+
if (b2 < 128) return toNum(l2, h, s);
|
|
1228
|
+
b2 = buf[p.pos++];
|
|
1229
|
+
h |= (b2 & 127) << 3;
|
|
1230
|
+
if (b2 < 128) return toNum(l2, h, s);
|
|
1231
|
+
b2 = buf[p.pos++];
|
|
1232
|
+
h |= (b2 & 127) << 10;
|
|
1233
|
+
if (b2 < 128) return toNum(l2, h, s);
|
|
1234
|
+
b2 = buf[p.pos++];
|
|
1235
|
+
h |= (b2 & 127) << 17;
|
|
1236
|
+
if (b2 < 128) return toNum(l2, h, s);
|
|
1237
|
+
b2 = buf[p.pos++];
|
|
1238
|
+
h |= (b2 & 127) << 24;
|
|
1239
|
+
if (b2 < 128) return toNum(l2, h, s);
|
|
1240
|
+
b2 = buf[p.pos++];
|
|
1241
|
+
h |= (b2 & 1) << 31;
|
|
1242
|
+
if (b2 < 128) return toNum(l2, h, s);
|
|
1243
|
+
throw new Error("Expected varint not more than 10 bytes");
|
|
1244
|
+
}
|
|
1245
|
+
function toNum(low, high, isSigned) {
|
|
1246
|
+
return isSigned ? high * 4294967296 + (low >>> 0) : (high >>> 0) * 4294967296 + (low >>> 0);
|
|
1247
|
+
}
|
|
1248
|
+
function writeBigVarint(val, pbf) {
|
|
1249
|
+
let low, high;
|
|
1250
|
+
if (val >= 0) {
|
|
1251
|
+
low = val % 4294967296 | 0;
|
|
1252
|
+
high = val / 4294967296 | 0;
|
|
1253
|
+
} else {
|
|
1254
|
+
low = ~(-val % 4294967296);
|
|
1255
|
+
high = ~(-val / 4294967296);
|
|
1256
|
+
if (low ^ 4294967295) {
|
|
1257
|
+
low = low + 1 | 0;
|
|
1258
|
+
} else {
|
|
1259
|
+
low = 0;
|
|
1260
|
+
high = high + 1 | 0;
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
if (val >= 18446744073709552e3 || val < -18446744073709552e3) {
|
|
1264
|
+
throw new Error("Given varint doesn't fit into 10 bytes");
|
|
1265
|
+
}
|
|
1266
|
+
pbf.realloc(10);
|
|
1267
|
+
writeBigVarintLow(low, high, pbf);
|
|
1268
|
+
writeBigVarintHigh(high, pbf);
|
|
1269
|
+
}
|
|
1270
|
+
function writeBigVarintLow(low, high, pbf) {
|
|
1271
|
+
pbf.buf[pbf.pos++] = low & 127 | 128;
|
|
1272
|
+
low >>>= 7;
|
|
1273
|
+
pbf.buf[pbf.pos++] = low & 127 | 128;
|
|
1274
|
+
low >>>= 7;
|
|
1275
|
+
pbf.buf[pbf.pos++] = low & 127 | 128;
|
|
1276
|
+
low >>>= 7;
|
|
1277
|
+
pbf.buf[pbf.pos++] = low & 127 | 128;
|
|
1278
|
+
low >>>= 7;
|
|
1279
|
+
pbf.buf[pbf.pos] = low & 127;
|
|
1280
|
+
}
|
|
1281
|
+
function writeBigVarintHigh(high, pbf) {
|
|
1282
|
+
const lsb = (high & 7) << 4;
|
|
1283
|
+
pbf.buf[pbf.pos++] |= lsb | ((high >>>= 3) ? 128 : 0);
|
|
1284
|
+
if (!high) return;
|
|
1285
|
+
pbf.buf[pbf.pos++] = high & 127 | ((high >>>= 7) ? 128 : 0);
|
|
1286
|
+
if (!high) return;
|
|
1287
|
+
pbf.buf[pbf.pos++] = high & 127 | ((high >>>= 7) ? 128 : 0);
|
|
1288
|
+
if (!high) return;
|
|
1289
|
+
pbf.buf[pbf.pos++] = high & 127 | ((high >>>= 7) ? 128 : 0);
|
|
1290
|
+
if (!high) return;
|
|
1291
|
+
pbf.buf[pbf.pos++] = high & 127 | ((high >>>= 7) ? 128 : 0);
|
|
1292
|
+
if (!high) return;
|
|
1293
|
+
pbf.buf[pbf.pos++] = high & 127;
|
|
1294
|
+
}
|
|
1295
|
+
function makeRoomForExtraLength(startPos, len, pbf) {
|
|
1296
|
+
const extraLen = len <= 16383 ? 1 : len <= 2097151 ? 2 : len <= 268435455 ? 3 : Math.floor(Math.log(len) / (Math.LN2 * 7));
|
|
1297
|
+
pbf.realloc(extraLen);
|
|
1298
|
+
for (let i2 = pbf.pos - 1; i2 >= startPos; i2--) pbf.buf[i2 + extraLen] = pbf.buf[i2];
|
|
1299
|
+
}
|
|
1300
|
+
function writePackedVarint(arr, pbf) {
|
|
1301
|
+
for (let i2 = 0; i2 < arr.length; i2++) pbf.writeVarint(arr[i2]);
|
|
1302
|
+
}
|
|
1303
|
+
function writePackedSVarint(arr, pbf) {
|
|
1304
|
+
for (let i2 = 0; i2 < arr.length; i2++) pbf.writeSVarint(arr[i2]);
|
|
1305
|
+
}
|
|
1306
|
+
function writePackedFloat(arr, pbf) {
|
|
1307
|
+
for (let i2 = 0; i2 < arr.length; i2++) pbf.writeFloat(arr[i2]);
|
|
1308
|
+
}
|
|
1309
|
+
function writePackedDouble(arr, pbf) {
|
|
1310
|
+
for (let i2 = 0; i2 < arr.length; i2++) pbf.writeDouble(arr[i2]);
|
|
1311
|
+
}
|
|
1312
|
+
function writePackedBoolean(arr, pbf) {
|
|
1313
|
+
for (let i2 = 0; i2 < arr.length; i2++) pbf.writeBoolean(arr[i2]);
|
|
1314
|
+
}
|
|
1315
|
+
function writePackedFixed32(arr, pbf) {
|
|
1316
|
+
for (let i2 = 0; i2 < arr.length; i2++) pbf.writeFixed32(arr[i2]);
|
|
1317
|
+
}
|
|
1318
|
+
function writePackedSFixed32(arr, pbf) {
|
|
1319
|
+
for (let i2 = 0; i2 < arr.length; i2++) pbf.writeSFixed32(arr[i2]);
|
|
1320
|
+
}
|
|
1321
|
+
function writePackedFixed64(arr, pbf) {
|
|
1322
|
+
for (let i2 = 0; i2 < arr.length; i2++) pbf.writeFixed64(arr[i2]);
|
|
1323
|
+
}
|
|
1324
|
+
function writePackedSFixed64(arr, pbf) {
|
|
1325
|
+
for (let i2 = 0; i2 < arr.length; i2++) pbf.writeSFixed64(arr[i2]);
|
|
1326
|
+
}
|
|
1327
|
+
function readUtf8(buf, pos, end) {
|
|
1328
|
+
let str = "";
|
|
1329
|
+
let i2 = pos;
|
|
1330
|
+
while (i2 < end) {
|
|
1331
|
+
const b0 = buf[i2];
|
|
1332
|
+
let c = null;
|
|
1333
|
+
let bytesPerSequence = b0 > 239 ? 4 : b0 > 223 ? 3 : b0 > 191 ? 2 : 1;
|
|
1334
|
+
if (i2 + bytesPerSequence > end) break;
|
|
1335
|
+
let b1, b2, b3;
|
|
1336
|
+
if (bytesPerSequence === 1) {
|
|
1337
|
+
if (b0 < 128) {
|
|
1338
|
+
c = b0;
|
|
1339
|
+
}
|
|
1340
|
+
} else if (bytesPerSequence === 2) {
|
|
1341
|
+
b1 = buf[i2 + 1];
|
|
1342
|
+
if ((b1 & 192) === 128) {
|
|
1343
|
+
c = (b0 & 31) << 6 | b1 & 63;
|
|
1344
|
+
if (c <= 127) {
|
|
1345
|
+
c = null;
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
} else if (bytesPerSequence === 3) {
|
|
1349
|
+
b1 = buf[i2 + 1];
|
|
1350
|
+
b2 = buf[i2 + 2];
|
|
1351
|
+
if ((b1 & 192) === 128 && (b2 & 192) === 128) {
|
|
1352
|
+
c = (b0 & 15) << 12 | (b1 & 63) << 6 | b2 & 63;
|
|
1353
|
+
if (c <= 2047 || c >= 55296 && c <= 57343) {
|
|
1354
|
+
c = null;
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
} else if (bytesPerSequence === 4) {
|
|
1358
|
+
b1 = buf[i2 + 1];
|
|
1359
|
+
b2 = buf[i2 + 2];
|
|
1360
|
+
b3 = buf[i2 + 3];
|
|
1361
|
+
if ((b1 & 192) === 128 && (b2 & 192) === 128 && (b3 & 192) === 128) {
|
|
1362
|
+
c = (b0 & 15) << 18 | (b1 & 63) << 12 | (b2 & 63) << 6 | b3 & 63;
|
|
1363
|
+
if (c <= 65535 || c >= 1114112) {
|
|
1364
|
+
c = null;
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
if (c === null) {
|
|
1369
|
+
c = 65533;
|
|
1370
|
+
bytesPerSequence = 1;
|
|
1371
|
+
} else if (c > 65535) {
|
|
1372
|
+
c -= 65536;
|
|
1373
|
+
str += String.fromCharCode(c >>> 10 & 1023 | 55296);
|
|
1374
|
+
c = 56320 | c & 1023;
|
|
1375
|
+
}
|
|
1376
|
+
str += String.fromCharCode(c);
|
|
1377
|
+
i2 += bytesPerSequence;
|
|
1378
|
+
}
|
|
1379
|
+
return str;
|
|
1380
|
+
}
|
|
1381
|
+
function writeUtf8(buf, str, pos) {
|
|
1382
|
+
for (let i2 = 0, c, lead; i2 < str.length; i2++) {
|
|
1383
|
+
c = str.charCodeAt(i2);
|
|
1384
|
+
if (c > 55295 && c < 57344) {
|
|
1385
|
+
if (lead) {
|
|
1386
|
+
if (c < 56320) {
|
|
1387
|
+
buf[pos++] = 239;
|
|
1388
|
+
buf[pos++] = 191;
|
|
1389
|
+
buf[pos++] = 189;
|
|
1390
|
+
lead = c;
|
|
1391
|
+
continue;
|
|
1392
|
+
} else {
|
|
1393
|
+
c = lead - 55296 << 10 | c - 56320 | 65536;
|
|
1394
|
+
lead = null;
|
|
1395
|
+
}
|
|
1396
|
+
} else {
|
|
1397
|
+
if (c > 56319 || i2 + 1 === str.length) {
|
|
1398
|
+
buf[pos++] = 239;
|
|
1399
|
+
buf[pos++] = 191;
|
|
1400
|
+
buf[pos++] = 189;
|
|
1401
|
+
} else {
|
|
1402
|
+
lead = c;
|
|
1403
|
+
}
|
|
1404
|
+
continue;
|
|
1405
|
+
}
|
|
1406
|
+
} else if (lead) {
|
|
1407
|
+
buf[pos++] = 239;
|
|
1408
|
+
buf[pos++] = 191;
|
|
1409
|
+
buf[pos++] = 189;
|
|
1410
|
+
lead = null;
|
|
1411
|
+
}
|
|
1412
|
+
if (c < 128) {
|
|
1413
|
+
buf[pos++] = c;
|
|
1414
|
+
} else {
|
|
1415
|
+
if (c < 2048) {
|
|
1416
|
+
buf[pos++] = c >> 6 | 192;
|
|
1417
|
+
} else {
|
|
1418
|
+
if (c < 65536) {
|
|
1419
|
+
buf[pos++] = c >> 12 | 224;
|
|
1420
|
+
} else {
|
|
1421
|
+
buf[pos++] = c >> 18 | 240;
|
|
1422
|
+
buf[pos++] = c >> 12 & 63 | 128;
|
|
1423
|
+
}
|
|
1424
|
+
buf[pos++] = c >> 6 & 63 | 128;
|
|
1425
|
+
}
|
|
1426
|
+
buf[pos++] = c & 63 | 128;
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
return pos;
|
|
1430
|
+
}
|
|
1431
|
+
var GeomType;
|
|
1432
|
+
(function(GeomType2) {
|
|
1433
|
+
GeomType2[GeomType2["UNKNOWN"] = 0] = "UNKNOWN";
|
|
1434
|
+
GeomType2[GeomType2["POINT"] = 1] = "POINT";
|
|
1435
|
+
GeomType2[GeomType2["LINESTRING"] = 2] = "LINESTRING";
|
|
1436
|
+
GeomType2[GeomType2["POLYGON"] = 3] = "POLYGON";
|
|
1437
|
+
})(GeomType || (GeomType = {}));
|
|
1438
|
+
function encodeVectorTile(tile) {
|
|
1439
|
+
const pbf = new Pbf();
|
|
1440
|
+
for (const id2 in tile.layers) {
|
|
1441
|
+
const layer = tile.layers[id2];
|
|
1442
|
+
if (!layer.extent) {
|
|
1443
|
+
layer.extent = tile.extent;
|
|
1444
|
+
}
|
|
1445
|
+
pbf.writeMessage(3, writeLayer, Object.assign(Object.assign({}, layer), { id: id2 }));
|
|
1446
|
+
}
|
|
1447
|
+
return pbf.finish();
|
|
1448
|
+
}
|
|
1449
|
+
function writeLayer(layer, pbf) {
|
|
1450
|
+
if (!pbf)
|
|
1451
|
+
throw new Error("pbf undefined");
|
|
1452
|
+
pbf.writeVarintField(15, 2);
|
|
1453
|
+
pbf.writeStringField(1, layer.id || "");
|
|
1454
|
+
pbf.writeVarintField(5, layer.extent || 4096);
|
|
1455
|
+
const context = {
|
|
1456
|
+
keys: [],
|
|
1457
|
+
values: [],
|
|
1458
|
+
keycache: {},
|
|
1459
|
+
valuecache: {}
|
|
1460
|
+
};
|
|
1461
|
+
for (const feature of layer.features) {
|
|
1462
|
+
context.feature = feature;
|
|
1463
|
+
pbf.writeMessage(2, writeFeature, context);
|
|
1464
|
+
}
|
|
1465
|
+
for (const key of context.keys) {
|
|
1466
|
+
pbf.writeStringField(3, key);
|
|
1467
|
+
}
|
|
1468
|
+
for (const value of context.values) {
|
|
1469
|
+
pbf.writeMessage(4, writeValue, value);
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
function writeFeature(context, pbf) {
|
|
1473
|
+
const feature = context.feature;
|
|
1474
|
+
if (!feature || !pbf)
|
|
1475
|
+
throw new Error();
|
|
1476
|
+
pbf.writeMessage(2, writeProperties, context);
|
|
1477
|
+
pbf.writeVarintField(3, feature.type);
|
|
1478
|
+
pbf.writeMessage(4, writeGeometry, feature);
|
|
1479
|
+
}
|
|
1480
|
+
function writeProperties(context, pbf) {
|
|
1481
|
+
const feature = context.feature;
|
|
1482
|
+
if (!feature || !pbf)
|
|
1483
|
+
throw new Error();
|
|
1484
|
+
const keys = context.keys;
|
|
1485
|
+
const values = context.values;
|
|
1486
|
+
const keycache = context.keycache;
|
|
1487
|
+
const valuecache = context.valuecache;
|
|
1488
|
+
for (const key in feature.properties) {
|
|
1489
|
+
let value = feature.properties[key];
|
|
1490
|
+
let keyIndex = keycache[key];
|
|
1491
|
+
if (value === null)
|
|
1492
|
+
continue;
|
|
1493
|
+
if (typeof keyIndex === "undefined") {
|
|
1494
|
+
keys.push(key);
|
|
1495
|
+
keyIndex = keys.length - 1;
|
|
1496
|
+
keycache[key] = keyIndex;
|
|
1497
|
+
}
|
|
1498
|
+
pbf.writeVarint(keyIndex);
|
|
1499
|
+
const type = typeof value;
|
|
1500
|
+
if (type !== "string" && type !== "boolean" && type !== "number") {
|
|
1501
|
+
value = JSON.stringify(value);
|
|
1502
|
+
}
|
|
1503
|
+
const valueKey = `${type}:${value}`;
|
|
1504
|
+
let valueIndex = valuecache[valueKey];
|
|
1505
|
+
if (typeof valueIndex === "undefined") {
|
|
1506
|
+
values.push(value);
|
|
1507
|
+
valueIndex = values.length - 1;
|
|
1508
|
+
valuecache[valueKey] = valueIndex;
|
|
1509
|
+
}
|
|
1510
|
+
pbf.writeVarint(valueIndex);
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
function command(cmd, length) {
|
|
1514
|
+
return (length << 3) + (cmd & 7);
|
|
1515
|
+
}
|
|
1516
|
+
function zigzag(num2) {
|
|
1517
|
+
return num2 << 1 ^ num2 >> 31;
|
|
1518
|
+
}
|
|
1519
|
+
function writeGeometry(feature, pbf) {
|
|
1520
|
+
if (!pbf)
|
|
1521
|
+
throw new Error();
|
|
1522
|
+
const geometry = feature.geometry;
|
|
1523
|
+
const type = feature.type;
|
|
1524
|
+
let x3 = 0;
|
|
1525
|
+
let y = 0;
|
|
1526
|
+
for (const ring of geometry) {
|
|
1527
|
+
let count = 1;
|
|
1528
|
+
if (type === GeomType.POINT) {
|
|
1529
|
+
count = ring.length / 2;
|
|
1530
|
+
}
|
|
1531
|
+
pbf.writeVarint(command(1, count));
|
|
1532
|
+
const length = ring.length / 2;
|
|
1533
|
+
const lineCount = type === GeomType.POLYGON ? length - 1 : length;
|
|
1534
|
+
for (let i2 = 0; i2 < lineCount; i2++) {
|
|
1535
|
+
if (i2 === 1 && type !== 1) {
|
|
1536
|
+
pbf.writeVarint(command(2, lineCount - 1));
|
|
1537
|
+
}
|
|
1538
|
+
const dx = ring[i2 * 2] - x3;
|
|
1539
|
+
const dy = ring[i2 * 2 + 1] - y;
|
|
1540
|
+
pbf.writeVarint(zigzag(dx));
|
|
1541
|
+
pbf.writeVarint(zigzag(dy));
|
|
1542
|
+
x3 += dx;
|
|
1543
|
+
y += dy;
|
|
1544
|
+
}
|
|
1545
|
+
if (type === GeomType.POLYGON) {
|
|
1546
|
+
pbf.writeVarint(command(7, 1));
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
function writeValue(value, pbf) {
|
|
1551
|
+
if (!pbf)
|
|
1552
|
+
throw new Error();
|
|
1553
|
+
if (typeof value === "string") {
|
|
1554
|
+
pbf.writeStringField(1, value);
|
|
1555
|
+
} else if (typeof value === "boolean") {
|
|
1556
|
+
pbf.writeBooleanField(7, value);
|
|
1557
|
+
} else if (typeof value === "number") {
|
|
1558
|
+
if (value % 1 !== 0) {
|
|
1559
|
+
pbf.writeDoubleField(3, value);
|
|
1560
|
+
} else if (value < 0) {
|
|
1561
|
+
pbf.writeSVarintField(6, value);
|
|
1562
|
+
} else {
|
|
1563
|
+
pbf.writeVarintField(5, value);
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
const perf = typeof performance !== "undefined" ? performance : void 0;
|
|
1568
|
+
const timeOrigin = perf ? perf.timeOrigin || (/* @__PURE__ */ new Date()).getTime() - perf.now() : (/* @__PURE__ */ new Date()).getTime();
|
|
1569
|
+
function getResourceTiming(url) {
|
|
1570
|
+
var _a2;
|
|
1571
|
+
return JSON.parse(JSON.stringify(((_a2 = perf === null || perf === void 0 ? void 0 : perf.getEntriesByName) === null || _a2 === void 0 ? void 0 : _a2.call(perf, url)) || []));
|
|
1572
|
+
}
|
|
1573
|
+
function now() {
|
|
1574
|
+
return perf ? perf.now() : (/* @__PURE__ */ new Date()).getTime();
|
|
1575
|
+
}
|
|
1576
|
+
function flatten(input) {
|
|
1577
|
+
const result = [];
|
|
1578
|
+
for (const list of input) {
|
|
1579
|
+
result.push(...list);
|
|
1580
|
+
}
|
|
1581
|
+
return result;
|
|
1582
|
+
}
|
|
1583
|
+
class Timer {
|
|
1584
|
+
constructor(name) {
|
|
1585
|
+
this.marks = {};
|
|
1586
|
+
this.urls = [];
|
|
1587
|
+
this.fetched = [];
|
|
1588
|
+
this.resources = [];
|
|
1589
|
+
this.tilesFetched = 0;
|
|
1590
|
+
this.timeOrigin = timeOrigin;
|
|
1591
|
+
this.finish = (url) => {
|
|
1592
|
+
this.markFinish();
|
|
1593
|
+
const get = (type) => {
|
|
1594
|
+
const all = this.marks[type] || [];
|
|
1595
|
+
const max2 = Math.max(...all.map((ns) => Math.max(...ns)));
|
|
1596
|
+
const min = Math.min(...all.map((ns) => Math.min(...ns)));
|
|
1597
|
+
return Number.isFinite(max2) ? max2 - min : void 0;
|
|
1598
|
+
};
|
|
1599
|
+
const duration = get("main") || 0;
|
|
1600
|
+
const fetch2 = get("fetch");
|
|
1601
|
+
const decode = get("decode");
|
|
1602
|
+
const process = get("isoline");
|
|
1603
|
+
return {
|
|
1604
|
+
url,
|
|
1605
|
+
tilesUsed: this.tilesFetched,
|
|
1606
|
+
origin: this.timeOrigin,
|
|
1607
|
+
marks: this.marks,
|
|
1608
|
+
resources: [
|
|
1609
|
+
...this.resources,
|
|
1610
|
+
...flatten(this.fetched.map(getResourceTiming))
|
|
1611
|
+
],
|
|
1612
|
+
duration,
|
|
1613
|
+
fetch: fetch2,
|
|
1614
|
+
decode,
|
|
1615
|
+
process,
|
|
1616
|
+
wait: duration - (fetch2 || 0) - (decode || 0) - (process || 0)
|
|
1617
|
+
};
|
|
1618
|
+
};
|
|
1619
|
+
this.error = (url) => Object.assign(Object.assign({}, this.finish(url)), { error: true });
|
|
1620
|
+
this.marker = (category) => {
|
|
1621
|
+
var _a2;
|
|
1622
|
+
if (!this.marks[category]) {
|
|
1623
|
+
this.marks[category] = [];
|
|
1624
|
+
}
|
|
1625
|
+
const marks = [now()];
|
|
1626
|
+
(_a2 = this.marks[category]) === null || _a2 === void 0 ? void 0 : _a2.push(marks);
|
|
1627
|
+
return () => marks.push(now());
|
|
1628
|
+
};
|
|
1629
|
+
this.useTile = (url) => {
|
|
1630
|
+
if (this.urls.indexOf(url) < 0) {
|
|
1631
|
+
this.urls.push(url);
|
|
1632
|
+
this.tilesFetched++;
|
|
1633
|
+
}
|
|
1634
|
+
};
|
|
1635
|
+
this.fetchTile = (url) => {
|
|
1636
|
+
if (this.fetched.indexOf(url) < 0) {
|
|
1637
|
+
this.fetched.push(url);
|
|
1638
|
+
}
|
|
1639
|
+
};
|
|
1640
|
+
this.addAll = (timings) => {
|
|
1641
|
+
var _a2;
|
|
1642
|
+
this.tilesFetched += timings.tilesUsed;
|
|
1643
|
+
const offset = timings.origin - this.timeOrigin;
|
|
1644
|
+
for (const category in timings.marks) {
|
|
1645
|
+
const key = category;
|
|
1646
|
+
const ourList = this.marks[key] || (this.marks[key] = []);
|
|
1647
|
+
ourList.push(...((_a2 = timings.marks[key]) === null || _a2 === void 0 ? void 0 : _a2.map((ns) => ns.map((n) => n + offset))) || []);
|
|
1648
|
+
}
|
|
1649
|
+
this.resources.push(...timings.resources.map((rt) => applyOffset(rt, offset)));
|
|
1650
|
+
};
|
|
1651
|
+
this.markFinish = this.marker(name);
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
const startOrEnd = /(Start$|End$|^start|^end)/;
|
|
1655
|
+
function applyOffset(obj, offset) {
|
|
1656
|
+
const result = {};
|
|
1657
|
+
for (const key in obj) {
|
|
1658
|
+
if (obj[key] !== 0 && startOrEnd.test(key)) {
|
|
1659
|
+
result[key] = Number(obj[key]) + offset;
|
|
1660
|
+
} else {
|
|
1661
|
+
result[key] = obj[key];
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
return result;
|
|
1665
|
+
}
|
|
1666
|
+
const defaultGetTile = (url, abortController) => __awaiter(void 0, void 0, void 0, function* () {
|
|
1667
|
+
const options = {
|
|
1668
|
+
signal: abortController.signal
|
|
1669
|
+
};
|
|
1670
|
+
const response = yield fetch(url, options);
|
|
1671
|
+
if (!response.ok) {
|
|
1672
|
+
throw new Error(`Bad response: ${response.status} for ${url}`);
|
|
1673
|
+
}
|
|
1674
|
+
return {
|
|
1675
|
+
data: yield response.blob(),
|
|
1676
|
+
expires: response.headers.get("expires") || void 0,
|
|
1677
|
+
cacheControl: response.headers.get("cache-control") || void 0
|
|
1678
|
+
};
|
|
1679
|
+
});
|
|
1680
|
+
class LocalDemManager {
|
|
1681
|
+
constructor(options) {
|
|
1682
|
+
this.loaded = Promise.resolve();
|
|
1683
|
+
this.fetchAndParseTile = (z, x3, y, abortController, timer) => {
|
|
1684
|
+
const self2 = this;
|
|
1685
|
+
const url = this.demUrlPattern.replace("{z}", z.toString()).replace("{x}", x3.toString()).replace("{y}", y.toString());
|
|
1686
|
+
timer === null || timer === void 0 ? void 0 : timer.useTile(url);
|
|
1687
|
+
return this.parsedCache.get(url, (_2, childAbortController) => __awaiter(this, void 0, void 0, function* () {
|
|
1688
|
+
const response = yield self2.fetchTile(z, x3, y, childAbortController, timer);
|
|
1689
|
+
if (isAborted(childAbortController))
|
|
1690
|
+
throw new Error("canceled");
|
|
1691
|
+
const promise = self2.decodeImage(response.data, self2.encoding, childAbortController);
|
|
1692
|
+
const mark = timer === null || timer === void 0 ? void 0 : timer.marker("decode");
|
|
1693
|
+
const result = yield promise;
|
|
1694
|
+
mark === null || mark === void 0 ? void 0 : mark();
|
|
1695
|
+
return result;
|
|
1696
|
+
}), abortController);
|
|
1697
|
+
};
|
|
1698
|
+
this.tileCache = new AsyncCache(options.cacheSize);
|
|
1699
|
+
this.parsedCache = new AsyncCache(options.cacheSize);
|
|
1700
|
+
this.contourCache = new AsyncCache(options.cacheSize);
|
|
1701
|
+
this.timeoutMs = options.timeoutMs;
|
|
1702
|
+
this.demUrlPattern = options.demUrlPattern;
|
|
1703
|
+
this.encoding = options.encoding;
|
|
1704
|
+
this.maxzoom = options.maxzoom;
|
|
1705
|
+
this.decodeImage = options.decodeImage || defaultDecoder;
|
|
1706
|
+
this.getTile = options.getTile || defaultGetTile;
|
|
1707
|
+
}
|
|
1708
|
+
fetchTile(z, x3, y, parentAbortController, timer) {
|
|
1709
|
+
const url = this.demUrlPattern.replace("{z}", z.toString()).replace("{x}", x3.toString()).replace("{y}", y.toString());
|
|
1710
|
+
timer === null || timer === void 0 ? void 0 : timer.useTile(url);
|
|
1711
|
+
return this.tileCache.get(url, (_2, childAbortController) => {
|
|
1712
|
+
timer === null || timer === void 0 ? void 0 : timer.fetchTile(url);
|
|
1713
|
+
const mark = timer === null || timer === void 0 ? void 0 : timer.marker("fetch");
|
|
1714
|
+
return withTimeout(this.timeoutMs, this.getTile(url, childAbortController).finally(() => mark === null || mark === void 0 ? void 0 : mark()), childAbortController);
|
|
1715
|
+
}, parentAbortController);
|
|
1716
|
+
}
|
|
1717
|
+
fetchDem(z, x3, y, options, abortController, timer) {
|
|
1718
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1719
|
+
const zoom = Math.min(z - (options.overzoom || 0), this.maxzoom);
|
|
1720
|
+
const subZ = z - zoom;
|
|
1721
|
+
const div = 1 << subZ;
|
|
1722
|
+
const newX = Math.floor(x3 / div);
|
|
1723
|
+
const newY = Math.floor(y / div);
|
|
1724
|
+
const tile = yield this.fetchAndParseTile(zoom, newX, newY, abortController, timer);
|
|
1725
|
+
return HeightTile.fromRawDem(tile).split(subZ, x3 % div, y % div);
|
|
1726
|
+
});
|
|
1727
|
+
}
|
|
1728
|
+
fetchContourTile(z, x3, y, options, parentAbortController, timer) {
|
|
1729
|
+
const { levels, multiplier = 1, buffer = 1, extent = 4096, contourLayer = "contours", elevationKey = "ele", levelKey = "level", subsampleBelow = 100 } = options;
|
|
1730
|
+
if (!levels || levels.length === 0) {
|
|
1731
|
+
return Promise.resolve({ arrayBuffer: new ArrayBuffer(0) });
|
|
1732
|
+
}
|
|
1733
|
+
const key = [z, x3, y, encodeIndividualOptions(options)].join("/");
|
|
1734
|
+
return this.contourCache.get(key, (_2, childAbortController) => __awaiter(this, void 0, void 0, function* () {
|
|
1735
|
+
const max2 = 1 << z;
|
|
1736
|
+
const neighborPromises = [];
|
|
1737
|
+
for (let iy = y - 1; iy <= y + 1; iy++) {
|
|
1738
|
+
for (let ix = x3 - 1; ix <= x3 + 1; ix++) {
|
|
1739
|
+
neighborPromises.push(iy < 0 || iy >= max2 ? void 0 : this.fetchDem(z, (ix + max2) % max2, iy, options, childAbortController, timer));
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
const neighbors = yield Promise.all(neighborPromises);
|
|
1743
|
+
let virtualTile = HeightTile.combineNeighbors(neighbors);
|
|
1744
|
+
if (!virtualTile || isAborted(childAbortController)) {
|
|
1745
|
+
return { arrayBuffer: new Uint8Array().buffer };
|
|
1746
|
+
}
|
|
1747
|
+
const mark = timer === null || timer === void 0 ? void 0 : timer.marker("isoline");
|
|
1748
|
+
if (virtualTile.width >= subsampleBelow) {
|
|
1749
|
+
virtualTile = virtualTile.materialize(2);
|
|
1750
|
+
} else {
|
|
1751
|
+
while (virtualTile.width < subsampleBelow) {
|
|
1752
|
+
virtualTile = virtualTile.subsamplePixelCenters(2).materialize(2);
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
virtualTile = virtualTile.averagePixelCentersToGrid().scaleElevation(multiplier).materialize(1);
|
|
1756
|
+
const isolines = generateIsolines(levels[0], virtualTile, extent, buffer);
|
|
1757
|
+
mark === null || mark === void 0 ? void 0 : mark();
|
|
1758
|
+
const result = encodeVectorTile({
|
|
1759
|
+
extent,
|
|
1760
|
+
layers: {
|
|
1761
|
+
[contourLayer]: {
|
|
1762
|
+
features: Object.entries(isolines).map(([eleString, geom]) => {
|
|
1763
|
+
const ele = Number(eleString);
|
|
1764
|
+
return {
|
|
1765
|
+
type: GeomType.LINESTRING,
|
|
1766
|
+
geometry: geom,
|
|
1767
|
+
properties: {
|
|
1768
|
+
[elevationKey]: ele,
|
|
1769
|
+
[levelKey]: Math.max(...levels.map((l2, i2) => ele % l2 === 0 ? i2 : 0))
|
|
1770
|
+
}
|
|
1771
|
+
};
|
|
1772
|
+
})
|
|
1773
|
+
}
|
|
1774
|
+
}
|
|
1775
|
+
});
|
|
1776
|
+
mark === null || mark === void 0 ? void 0 : mark();
|
|
1777
|
+
return { arrayBuffer: result.buffer };
|
|
1778
|
+
}), parentAbortController);
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
let id = 0;
|
|
1782
|
+
class Actor {
|
|
1783
|
+
constructor(dest, dispatcher, timeoutMs = 2e4) {
|
|
1784
|
+
this.callbacks = {};
|
|
1785
|
+
this.cancels = {};
|
|
1786
|
+
this.dest = dest;
|
|
1787
|
+
this.timeoutMs = timeoutMs;
|
|
1788
|
+
this.dest.onmessage = (_a2) => __awaiter(this, [_a2], void 0, function* ({ data }) {
|
|
1789
|
+
const message = data;
|
|
1790
|
+
if (message.type === "cancel") {
|
|
1791
|
+
const cancel = this.cancels[message.id];
|
|
1792
|
+
delete this.cancels[message.id];
|
|
1793
|
+
cancel === null || cancel === void 0 ? void 0 : cancel.abort();
|
|
1794
|
+
} else if (message.type === "response") {
|
|
1795
|
+
const callback = this.callbacks[message.id];
|
|
1796
|
+
delete this.callbacks[message.id];
|
|
1797
|
+
if (callback) {
|
|
1798
|
+
callback(message.error ? new Error(message.error) : void 0, message.response, message.timings);
|
|
1799
|
+
}
|
|
1800
|
+
} else if (message.type === "request") {
|
|
1801
|
+
const timer = new Timer("worker");
|
|
1802
|
+
const handler = dispatcher[message.name];
|
|
1803
|
+
const abortController = new AbortController();
|
|
1804
|
+
const request = handler.apply(handler, [
|
|
1805
|
+
...message.args,
|
|
1806
|
+
abortController,
|
|
1807
|
+
timer
|
|
1808
|
+
]);
|
|
1809
|
+
const url = `${message.name}_${message.id}`;
|
|
1810
|
+
if (message.id && request) {
|
|
1811
|
+
this.cancels[message.id] = abortController;
|
|
1812
|
+
try {
|
|
1813
|
+
const response = yield request;
|
|
1814
|
+
const transferrables = response === null || response === void 0 ? void 0 : response.transferrables;
|
|
1815
|
+
this.postMessage({
|
|
1816
|
+
id: message.id,
|
|
1817
|
+
type: "response",
|
|
1818
|
+
response,
|
|
1819
|
+
timings: timer.finish(url)
|
|
1820
|
+
}, transferrables);
|
|
1821
|
+
} catch (e) {
|
|
1822
|
+
this.postMessage({
|
|
1823
|
+
id: message.id,
|
|
1824
|
+
type: "response",
|
|
1825
|
+
error: (e === null || e === void 0 ? void 0 : e.toString()) || "error",
|
|
1826
|
+
timings: timer.finish(url)
|
|
1827
|
+
});
|
|
1828
|
+
}
|
|
1829
|
+
delete this.cancels[message.id];
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
});
|
|
1833
|
+
}
|
|
1834
|
+
postMessage(message, transferrables) {
|
|
1835
|
+
this.dest.postMessage(message, transferrables || []);
|
|
1836
|
+
}
|
|
1837
|
+
/** Invokes a method by name with a set of arguments in the remote context. */
|
|
1838
|
+
send(name, transferrables, abortController, timer, ...args) {
|
|
1839
|
+
const thisId = ++id;
|
|
1840
|
+
const value = new Promise((resolve, reject) => {
|
|
1841
|
+
this.postMessage({ id: thisId, type: "request", name, args }, transferrables);
|
|
1842
|
+
this.callbacks[thisId] = (error, result, timings) => {
|
|
1843
|
+
timer === null || timer === void 0 ? void 0 : timer.addAll(timings);
|
|
1844
|
+
if (error)
|
|
1845
|
+
reject(error);
|
|
1846
|
+
else
|
|
1847
|
+
resolve(result);
|
|
1848
|
+
};
|
|
1849
|
+
});
|
|
1850
|
+
onAbort(abortController, () => {
|
|
1851
|
+
delete this.callbacks[thisId];
|
|
1852
|
+
this.postMessage({ id: thisId, type: "cancel" });
|
|
1853
|
+
});
|
|
1854
|
+
return withTimeout(this.timeoutMs, value, abortController);
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
exports.A = Actor;
|
|
1858
|
+
exports.H = HeightTile;
|
|
1859
|
+
exports.L = LocalDemManager;
|
|
1860
|
+
exports.T = Timer;
|
|
1861
|
+
exports._ = __awaiter;
|
|
1862
|
+
exports.a = decodeOptions;
|
|
1863
|
+
exports.b = generateIsolines;
|
|
1864
|
+
exports.c = decodeParsedImage;
|
|
1865
|
+
exports.d = defaultDecoder;
|
|
1866
|
+
exports.e = encodeOptions;
|
|
1867
|
+
exports.f = prepareContourTile;
|
|
1868
|
+
exports.g = getOptionsForZoom;
|
|
1869
|
+
exports.p = prepareDemTile;
|
|
1870
|
+
}));
|
|
1871
|
+
define(["./shared"], (function(actor) {
|
|
1872
|
+
"use strict";
|
|
1873
|
+
const noManager = (managerId) => Promise.reject(new Error(`No manager registered for ${managerId}`));
|
|
1874
|
+
class WorkerDispatch {
|
|
1875
|
+
constructor() {
|
|
1876
|
+
this.managers = {};
|
|
1877
|
+
this.init = (message, _2) => {
|
|
1878
|
+
this.managers[message.managerId] = new actor.L(message);
|
|
1879
|
+
return Promise.resolve();
|
|
1880
|
+
};
|
|
1881
|
+
this.fetchTile = (managerId, z, x3, y, abortController, timer) => {
|
|
1882
|
+
var _a2;
|
|
1883
|
+
return ((_a2 = this.managers[managerId]) === null || _a2 === void 0 ? void 0 : _a2.fetchTile(z, x3, y, abortController, timer)) || noManager(managerId);
|
|
1884
|
+
};
|
|
1885
|
+
this.fetchAndParseTile = (managerId, z, x3, y, abortController, timer) => {
|
|
1886
|
+
var _a2;
|
|
1887
|
+
return actor.p(((_a2 = this.managers[managerId]) === null || _a2 === void 0 ? void 0 : _a2.fetchAndParseTile(z, x3, y, abortController, timer)) || noManager(managerId), true);
|
|
1888
|
+
};
|
|
1889
|
+
this.fetchContourTile = (managerId, z, x3, y, options, abortController, timer) => {
|
|
1890
|
+
var _a2;
|
|
1891
|
+
return actor.f(((_a2 = this.managers[managerId]) === null || _a2 === void 0 ? void 0 : _a2.fetchContourTile(z, x3, y, options, abortController, timer)) || noManager(managerId));
|
|
1892
|
+
};
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
const g2 = typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : global;
|
|
1896
|
+
g2.actor = new actor.A(g2, new WorkerDispatch());
|
|
1897
|
+
}));
|
|
1898
|
+
define(["./shared"], (function(actor) {
|
|
1899
|
+
"use strict";
|
|
1900
|
+
const CONFIG = { workerUrl: "" };
|
|
1901
|
+
let _actor;
|
|
1902
|
+
let id = 0;
|
|
1903
|
+
class MainThreadDispatch {
|
|
1904
|
+
constructor() {
|
|
1905
|
+
this.decodeImage = (blob, encoding, abortController) => actor.p(actor.d(blob, encoding, abortController), false);
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
function defaultActor() {
|
|
1909
|
+
if (!_actor) {
|
|
1910
|
+
const worker2 = new Worker(CONFIG.workerUrl);
|
|
1911
|
+
const dispatch = new MainThreadDispatch();
|
|
1912
|
+
_actor = new actor.A(worker2, dispatch);
|
|
1913
|
+
}
|
|
1914
|
+
return _actor;
|
|
1915
|
+
}
|
|
1916
|
+
class RemoteDemManager {
|
|
1917
|
+
constructor(options) {
|
|
1918
|
+
this.fetchTile = (z, x3, y, abortController, timer) => this.actor.send("fetchTile", [], abortController, timer, this.managerId, z, x3, y);
|
|
1919
|
+
this.fetchAndParseTile = (z, x3, y, abortController, timer) => this.actor.send("fetchAndParseTile", [], abortController, timer, this.managerId, z, x3, y);
|
|
1920
|
+
this.fetchContourTile = (z, x3, y, options2, abortController, timer) => this.actor.send("fetchContourTile", [], abortController, timer, this.managerId, z, x3, y, options2);
|
|
1921
|
+
const managerId = this.managerId = ++id;
|
|
1922
|
+
this.actor = options.actor || defaultActor();
|
|
1923
|
+
this.loaded = this.actor.send("init", [], new AbortController(), void 0, Object.assign(Object.assign({}, options), { managerId }));
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
if (!Blob.prototype.arrayBuffer) {
|
|
1927
|
+
Blob.prototype.arrayBuffer = function arrayBuffer() {
|
|
1928
|
+
return new Promise((resolve, reject) => {
|
|
1929
|
+
const fileReader = new FileReader();
|
|
1930
|
+
fileReader.onload = (event) => {
|
|
1931
|
+
var _a2;
|
|
1932
|
+
return resolve((_a2 = event.target) === null || _a2 === void 0 ? void 0 : _a2.result);
|
|
1933
|
+
};
|
|
1934
|
+
fileReader.onerror = reject;
|
|
1935
|
+
fileReader.readAsArrayBuffer(this);
|
|
1936
|
+
});
|
|
1937
|
+
};
|
|
1938
|
+
}
|
|
1939
|
+
const v3compat = (v4) => (requestParameters, arg2) => {
|
|
1940
|
+
if (arg2 instanceof AbortController) {
|
|
1941
|
+
return v4(requestParameters, arg2);
|
|
1942
|
+
} else {
|
|
1943
|
+
const abortController = new AbortController();
|
|
1944
|
+
v4(requestParameters, abortController).then((result) => arg2(void 0, result.data, result.cacheControl, result.expires), (err2) => arg2(err2)).catch((err2) => arg2(err2));
|
|
1945
|
+
return { cancel: () => abortController.abort() };
|
|
1946
|
+
}
|
|
1947
|
+
};
|
|
1948
|
+
const used = /* @__PURE__ */ new Set();
|
|
1949
|
+
class DemSource {
|
|
1950
|
+
constructor({ url, cacheSize = 100, id: id2 = "dem", encoding = "terrarium", maxzoom = 12, worker: worker2 = true, timeoutMs = 1e4, actor: actor$1 }) {
|
|
1951
|
+
this.timingCallbacks = [];
|
|
1952
|
+
this.onTiming = (callback) => {
|
|
1953
|
+
this.timingCallbacks.push(callback);
|
|
1954
|
+
};
|
|
1955
|
+
this.setupMaplibre = (maplibre) => {
|
|
1956
|
+
maplibre.addProtocol(this.sharedDemProtocolId, this.sharedDemProtocol);
|
|
1957
|
+
maplibre.addProtocol(this.contourProtocolId, this.contourProtocol);
|
|
1958
|
+
};
|
|
1959
|
+
this.sharedDemProtocolV4 = (request, abortController) => actor._(this, void 0, void 0, function* () {
|
|
1960
|
+
const [z, x3, y] = this.parseUrl(request.url);
|
|
1961
|
+
const timer = new actor.T("main");
|
|
1962
|
+
let timing;
|
|
1963
|
+
try {
|
|
1964
|
+
const data = yield this.manager.fetchTile(z, x3, y, abortController, timer);
|
|
1965
|
+
timing = timer.finish(request.url);
|
|
1966
|
+
const arrayBuffer = yield data.data.arrayBuffer();
|
|
1967
|
+
return {
|
|
1968
|
+
data: arrayBuffer,
|
|
1969
|
+
cacheControl: data.cacheControl,
|
|
1970
|
+
expires: data.expires
|
|
1971
|
+
};
|
|
1972
|
+
} catch (error) {
|
|
1973
|
+
timing = timer.error(request.url);
|
|
1974
|
+
throw error;
|
|
1975
|
+
} finally {
|
|
1976
|
+
this.timingCallbacks.forEach((cb) => cb(timing));
|
|
1977
|
+
}
|
|
1978
|
+
});
|
|
1979
|
+
this.contourProtocolV4 = (request, abortController) => actor._(this, void 0, void 0, function* () {
|
|
1980
|
+
const timer = new actor.T("main");
|
|
1981
|
+
let timing;
|
|
1982
|
+
try {
|
|
1983
|
+
const [z, x3, y] = this.parseUrl(request.url);
|
|
1984
|
+
const options = actor.a(request.url);
|
|
1985
|
+
const data = yield this.manager.fetchContourTile(z, x3, y, actor.g(options, z), abortController, timer);
|
|
1986
|
+
timing = timer.finish(request.url);
|
|
1987
|
+
return { data: data.arrayBuffer };
|
|
1988
|
+
} catch (error) {
|
|
1989
|
+
timing = timer.error(request.url);
|
|
1990
|
+
throw error;
|
|
1991
|
+
} finally {
|
|
1992
|
+
this.timingCallbacks.forEach((cb) => cb(timing));
|
|
1993
|
+
}
|
|
1994
|
+
});
|
|
1995
|
+
this.contourProtocol = v3compat(this.contourProtocolV4);
|
|
1996
|
+
this.sharedDemProtocol = v3compat(this.sharedDemProtocolV4);
|
|
1997
|
+
this.contourProtocolUrl = (options) => `${this.contourProtocolUrlBase}?${actor.e(options)}`;
|
|
1998
|
+
let protocolPrefix2 = id2;
|
|
1999
|
+
let i2 = 1;
|
|
2000
|
+
while (used.has(protocolPrefix2)) {
|
|
2001
|
+
protocolPrefix2 = id2 + i2++;
|
|
2002
|
+
}
|
|
2003
|
+
used.add(protocolPrefix2);
|
|
2004
|
+
this.sharedDemProtocolId = `${protocolPrefix2}-shared`;
|
|
2005
|
+
this.contourProtocolId = `${protocolPrefix2}-contour`;
|
|
2006
|
+
this.sharedDemProtocolUrl = `${this.sharedDemProtocolId}://{z}/{x}/{y}`;
|
|
2007
|
+
this.contourProtocolUrlBase = `${this.contourProtocolId}://{z}/{x}/{y}`;
|
|
2008
|
+
const ManagerClass = worker2 ? RemoteDemManager : actor.L;
|
|
2009
|
+
this.manager = new ManagerClass({
|
|
2010
|
+
demUrlPattern: url,
|
|
2011
|
+
cacheSize,
|
|
2012
|
+
encoding,
|
|
2013
|
+
maxzoom,
|
|
2014
|
+
timeoutMs,
|
|
2015
|
+
actor: actor$1
|
|
2016
|
+
});
|
|
2017
|
+
}
|
|
2018
|
+
getDemTile(z, x3, y, abortController) {
|
|
2019
|
+
return this.manager.fetchAndParseTile(z, x3, y, abortController || new AbortController());
|
|
2020
|
+
}
|
|
2021
|
+
parseUrl(url) {
|
|
2022
|
+
const [, z, x3, y] = /\/\/(\d+)\/(\d+)\/(\d+)/.exec(url) || [];
|
|
2023
|
+
return [Number(z), Number(x3), Number(y)];
|
|
2024
|
+
}
|
|
2025
|
+
}
|
|
2026
|
+
const exported = {
|
|
2027
|
+
generateIsolines: actor.b,
|
|
2028
|
+
DemSource,
|
|
2029
|
+
HeightTile: actor.H,
|
|
2030
|
+
LocalDemManager: actor.L,
|
|
2031
|
+
decodeParsedImage: actor.c,
|
|
2032
|
+
set workerUrl(url) {
|
|
2033
|
+
CONFIG.workerUrl = url;
|
|
2034
|
+
},
|
|
2035
|
+
get workerUrl() {
|
|
2036
|
+
return CONFIG.workerUrl;
|
|
2037
|
+
}
|
|
2038
|
+
};
|
|
2039
|
+
return exported;
|
|
2040
|
+
}));
|
|
2041
|
+
mlcontour$1 = mlcontour;
|
|
2042
|
+
}
|
|
2043
|
+
});
|
|
2044
|
+
|
|
2045
|
+
// src/browser.ts
|
|
2046
|
+
init_portable_identity_rules();
|
|
2047
|
+
|
|
1
2048
|
// src/runtime.ts
|
|
2049
|
+
var defaultTileflowRuntimeView = Object.freeze({
|
|
2050
|
+
bearing: 0,
|
|
2051
|
+
center: [0, 20],
|
|
2052
|
+
pitch: 0,
|
|
2053
|
+
zoom: 2
|
|
2054
|
+
});
|
|
2055
|
+
var tileflowSurfaceIdPattern = /^[a-z0-9](?:[a-z0-9._-]{0,62}[a-z0-9])?$/u;
|
|
2056
|
+
function normalizeTileflowSurfaceId(value) {
|
|
2057
|
+
return typeof value === "string" && tileflowSurfaceIdPattern.test(value) ? value : "default";
|
|
2058
|
+
}
|
|
2059
|
+
var tileflowStyleFontFacesMetadataKey = "tileflow:fontFaces";
|
|
2060
|
+
var tileflowStyleFontFaceLimits = Object.freeze({
|
|
2061
|
+
maximumCount: 16,
|
|
2062
|
+
maximumSourceLength: 2048
|
|
2063
|
+
});
|
|
2
2064
|
var maxStaticImagePixels = 1280 * 1280;
|
|
2065
|
+
var maximumManifestBytes = 1024 * 1024;
|
|
2066
|
+
var maximumSessionGrantBytes = 64 * 1024;
|
|
3
2067
|
var manifestCache = new globalThis.Map();
|
|
2068
|
+
function getTileflowStyleFontFaces(style) {
|
|
2069
|
+
const input = style.metadata?.[tileflowStyleFontFacesMetadataKey];
|
|
2070
|
+
if (input === void 0) return [];
|
|
2071
|
+
if (!Array.isArray(input) || input.length > tileflowStyleFontFaceLimits.maximumCount) {
|
|
2072
|
+
throw new TypeError("Invalid Tileflow style fontFaces metadata.");
|
|
2073
|
+
}
|
|
2074
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2075
|
+
return input.map((value) => {
|
|
2076
|
+
if (!isPlainRuntimeRecord(value)) {
|
|
2077
|
+
throw new TypeError("Invalid Tileflow style fontFaces metadata.");
|
|
2078
|
+
}
|
|
2079
|
+
const keys = Object.keys(value);
|
|
2080
|
+
if (keys.some((key2) => !["family", "source", "style", "weight"].includes(key2))) {
|
|
2081
|
+
throw new TypeError("Invalid Tileflow style fontFaces metadata.");
|
|
2082
|
+
}
|
|
2083
|
+
const family = value.family;
|
|
2084
|
+
const source = value.source;
|
|
2085
|
+
const fontStyle = value.style;
|
|
2086
|
+
const weight = value.weight;
|
|
2087
|
+
if (typeof family !== "string" || family.length === 0 || family.length > 100 || family !== family.trim() || /[\p{Cc}\\]/u.test(family) || typeof source !== "string" || source.length === 0 || source.length > tileflowStyleFontFaceLimits.maximumSourceLength || source !== source.trim() || /[\p{Cc}\\]/u.test(source) || !isTileflowPublicFontUrl(source) || fontStyle !== void 0 && !["italic", "normal", "oblique"].includes(String(fontStyle)) || weight !== void 0 && !/^[1-9]00$/u.test(String(weight))) {
|
|
2088
|
+
throw new TypeError("Invalid Tileflow style fontFaces metadata.");
|
|
2089
|
+
}
|
|
2090
|
+
const face = {
|
|
2091
|
+
family,
|
|
2092
|
+
source,
|
|
2093
|
+
...fontStyle === void 0 ? {} : { style: fontStyle },
|
|
2094
|
+
...weight === void 0 ? {} : { weight }
|
|
2095
|
+
};
|
|
2096
|
+
const key = `${face.family}\0${face.style ?? "normal"}\0${face.weight ?? "400"}`;
|
|
2097
|
+
if (seen.has(key)) throw new TypeError("Invalid duplicate Tileflow style fontFace metadata.");
|
|
2098
|
+
seen.add(key);
|
|
2099
|
+
return face;
|
|
2100
|
+
});
|
|
2101
|
+
}
|
|
4
2102
|
function normalizeTileflowUrl(value) {
|
|
5
2103
|
return value.replace(/\/+$/, "");
|
|
6
2104
|
}
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
|
|
2105
|
+
function isRecord(value) {
|
|
2106
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
2107
|
+
}
|
|
2108
|
+
function isPlainRuntimeRecord(value) {
|
|
2109
|
+
if (!isRecord(value)) return false;
|
|
2110
|
+
const prototype = Object.getPrototypeOf(value);
|
|
2111
|
+
return prototype === Object.prototype || prototype === null;
|
|
2112
|
+
}
|
|
2113
|
+
function isTileflowPublicFontUrl(value) {
|
|
2114
|
+
if (value.startsWith("//")) return false;
|
|
2115
|
+
if (value.startsWith("/") || value.startsWith("./") || value.startsWith("../")) return true;
|
|
2116
|
+
try {
|
|
2117
|
+
const url = new URL(value);
|
|
2118
|
+
return (url.protocol === "http:" || url.protocol === "https:") && !url.username && !url.password && !url.hash;
|
|
2119
|
+
} catch {
|
|
2120
|
+
return false;
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
function resolveTileflowAnalyticsRequestUrl(url, analytics, sessionId) {
|
|
2124
|
+
if (!analytics || analytics.enabled === false) {
|
|
2125
|
+
return void 0;
|
|
2126
|
+
}
|
|
2127
|
+
const apiUrl = getTileflowAnalyticsApiUrl(analytics);
|
|
2128
|
+
if (!apiUrl || !isTileflowTileUrl(url, apiUrl)) {
|
|
2129
|
+
return void 0;
|
|
2130
|
+
}
|
|
2131
|
+
const nextUrl = new URL(url);
|
|
2132
|
+
nextUrl.searchParams.set("session", sessionId);
|
|
2133
|
+
if (analytics.mapId) {
|
|
2134
|
+
nextUrl.searchParams.set("map", analytics.mapId);
|
|
2135
|
+
}
|
|
2136
|
+
if (analytics.styleId) {
|
|
2137
|
+
nextUrl.searchParams.set("styleId", analytics.styleId);
|
|
2138
|
+
}
|
|
2139
|
+
return nextUrl.toString();
|
|
2140
|
+
}
|
|
2141
|
+
function startTileflowSession(analytics, input) {
|
|
2142
|
+
if (!analytics || analytics.enabled === false || !input.mapId || typeof navigator === "undefined") {
|
|
2143
|
+
return;
|
|
2144
|
+
}
|
|
2145
|
+
const endpoint = analytics.endpoint ?? `${getTileflowAnalyticsApiUrl(analytics)}/v1/sessions/analytics`;
|
|
2146
|
+
const payload = JSON.stringify({
|
|
2147
|
+
mapId: input.mapId,
|
|
2148
|
+
metadata: analytics.metadata,
|
|
2149
|
+
referrer: typeof document === "undefined" ? void 0 : document.referrer || "",
|
|
2150
|
+
sdkVersion: analytics.sdkVersion,
|
|
2151
|
+
sessionId: input.sessionId,
|
|
2152
|
+
source: analytics.source ?? input.source,
|
|
2153
|
+
styleId: input.styleId,
|
|
2154
|
+
surfaceId: normalizeTileflowSurfaceId(analytics.surfaceId),
|
|
2155
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
2156
|
+
});
|
|
2157
|
+
const blob = new Blob([payload], { type: "application/json" });
|
|
2158
|
+
if (navigator.sendBeacon?.(endpoint, blob)) {
|
|
2159
|
+
return;
|
|
2160
|
+
}
|
|
2161
|
+
void fetch(endpoint, {
|
|
2162
|
+
body: payload,
|
|
2163
|
+
credentials: "omit",
|
|
2164
|
+
headers: {
|
|
2165
|
+
"Content-Type": "application/json"
|
|
2166
|
+
},
|
|
2167
|
+
keepalive: true,
|
|
2168
|
+
method: "POST"
|
|
2169
|
+
}).catch(() => void 0);
|
|
2170
|
+
}
|
|
2171
|
+
function getTileflowAnalyticsApiUrl(analytics) {
|
|
2172
|
+
if (analytics.apiUrl) {
|
|
2173
|
+
return normalizeTileflowUrl(analytics.apiUrl);
|
|
2174
|
+
}
|
|
2175
|
+
if (analytics.endpoint) {
|
|
2176
|
+
try {
|
|
2177
|
+
const url = new URL(analytics.endpoint, getTileflowLocationOrigin());
|
|
2178
|
+
return url.origin;
|
|
2179
|
+
} catch {
|
|
2180
|
+
return "https://api.tileflow.dev";
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
return "https://api.tileflow.dev";
|
|
2184
|
+
}
|
|
2185
|
+
function isTileflowTileUrl(url, apiUrl) {
|
|
2186
|
+
try {
|
|
2187
|
+
const parsedUrl = new URL(url);
|
|
2188
|
+
const parsedApiUrl = new URL(apiUrl);
|
|
2189
|
+
return parsedUrl.origin === parsedApiUrl.origin && isTilePath(parsedUrl);
|
|
2190
|
+
} catch {
|
|
2191
|
+
return false;
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
function isTilePath(url) {
|
|
2195
|
+
return url.pathname.startsWith("/tiles/") || url.pathname.startsWith("/v1/tiles/");
|
|
2196
|
+
}
|
|
2197
|
+
function getTileflowLocationOrigin() {
|
|
2198
|
+
if (typeof window === "undefined") {
|
|
2199
|
+
return "http://localhost";
|
|
2200
|
+
}
|
|
2201
|
+
return window.location.origin;
|
|
2202
|
+
}
|
|
2203
|
+
|
|
2204
|
+
// src/data/world-release-id-values.ts
|
|
2205
|
+
var tileflowWorldReleaseIdMinimumLength = 12;
|
|
2206
|
+
var tileflowWorldReleaseIdMaximumLength = 128;
|
|
2207
|
+
var tileflowWorldReleaseIdPatternSource = "^world-v1-[a-z0-9][a-z0-9._-]*[a-z0-9]$";
|
|
2208
|
+
var tileflowWorldReleaseIdPattern = new RegExp(tileflowWorldReleaseIdPatternSource, "u");
|
|
2209
|
+
function isTileflowWorldReleaseId(value) {
|
|
2210
|
+
return typeof value === "string" && value.length >= tileflowWorldReleaseIdMinimumLength && value.length <= tileflowWorldReleaseIdMaximumLength && tileflowWorldReleaseIdPattern.test(value);
|
|
2211
|
+
}
|
|
2212
|
+
|
|
2213
|
+
// src/fair-use-browser.ts
|
|
2214
|
+
var protocol = "tileflow-world";
|
|
2215
|
+
var defaultHelpUrl = "https://tileflow.dev/world/connect";
|
|
2216
|
+
var ownerAction = "Site owner: manage this map with Tileflow.";
|
|
2217
|
+
var maximumWorldTileBytes = 16 * 1024 * 1024;
|
|
2218
|
+
var oversizedWorldTileMessage = "Tileflow World tile exceeds the maximum response size";
|
|
2219
|
+
var unreadableWorldTileMessage = "Tileflow World tile response could not be read";
|
|
2220
|
+
var registrations = /* @__PURE__ */ new Map();
|
|
2221
|
+
var installed = false;
|
|
2222
|
+
var channelSequence = 0;
|
|
2223
|
+
function registerTileflowWorldRequestBridge(input) {
|
|
2224
|
+
if (!installed) {
|
|
2225
|
+
input.addProtocol(protocol, loadTileflowWorldRequest);
|
|
2226
|
+
installed = true;
|
|
2227
|
+
}
|
|
2228
|
+
const channel = createChannel();
|
|
2229
|
+
registrations.set(channel, {
|
|
2230
|
+
currentNoticeState: null,
|
|
2231
|
+
fetch: input.fetch ?? globalThis.fetch,
|
|
2232
|
+
lastNoticeSequence: 0,
|
|
2233
|
+
nextRequestSequence: 0,
|
|
2234
|
+
onNotice: input.onNotice
|
|
2235
|
+
});
|
|
2236
|
+
let disposed = false;
|
|
2237
|
+
return Object.freeze({
|
|
2238
|
+
dispose() {
|
|
2239
|
+
if (disposed) return;
|
|
2240
|
+
disposed = true;
|
|
2241
|
+
registrations.delete(channel);
|
|
2242
|
+
},
|
|
2243
|
+
rewriteUrl(url) {
|
|
2244
|
+
if (disposed || !isCanonicalWorldTileUrl(url)) return url;
|
|
2245
|
+
return `${protocol}://request/${channel}?url=${encodeURIComponent(url)}`;
|
|
2246
|
+
}
|
|
2247
|
+
});
|
|
2248
|
+
}
|
|
2249
|
+
function tileflowFairUseNoticeMessage(state) {
|
|
2250
|
+
return state === "MANAGED_REQUIRED" ? "Map usage is temporarily limited." : "Map usage is approaching its temporary limit.";
|
|
2251
|
+
}
|
|
2252
|
+
function attachTileflowFairUseNotice(container) {
|
|
2253
|
+
let element = null;
|
|
2254
|
+
let current = null;
|
|
2255
|
+
let disposed = false;
|
|
2256
|
+
const remove = () => {
|
|
2257
|
+
element?.remove();
|
|
2258
|
+
element = null;
|
|
2259
|
+
current = null;
|
|
2260
|
+
};
|
|
2261
|
+
return Object.freeze({
|
|
2262
|
+
dispose() {
|
|
2263
|
+
if (disposed) return;
|
|
2264
|
+
disposed = true;
|
|
2265
|
+
remove();
|
|
2266
|
+
},
|
|
2267
|
+
update(notice) {
|
|
2268
|
+
if (disposed) return;
|
|
2269
|
+
if (!notice) {
|
|
2270
|
+
remove();
|
|
2271
|
+
return;
|
|
2272
|
+
}
|
|
2273
|
+
if (current?.state === "MANAGED_REQUIRED" && notice.state === "GRACE") return;
|
|
2274
|
+
if (element && current?.state !== notice.state) {
|
|
2275
|
+
element.remove();
|
|
2276
|
+
element = null;
|
|
2277
|
+
}
|
|
2278
|
+
current = notice;
|
|
2279
|
+
if (!element) {
|
|
2280
|
+
element = container.ownerDocument.createElement("div");
|
|
2281
|
+
element.dataset.tileflowFairUseNotice = "";
|
|
2282
|
+
element.setAttribute("aria-atomic", "true");
|
|
2283
|
+
element.setAttribute("aria-live", "polite");
|
|
2284
|
+
element.setAttribute("role", "status");
|
|
2285
|
+
Object.assign(element.style, noticeContainerStyles(notice.state));
|
|
2286
|
+
container.append(element);
|
|
2287
|
+
}
|
|
2288
|
+
element.dataset.tileflowFairUseNotice = notice.state.toLowerCase().replace("_", "-");
|
|
2289
|
+
element.replaceChildren();
|
|
2290
|
+
const indicator = container.ownerDocument.createElement("span");
|
|
2291
|
+
indicator.setAttribute("aria-hidden", "true");
|
|
2292
|
+
indicator.textContent = notice.state === "MANAGED_REQUIRED" ? "!" : "";
|
|
2293
|
+
Object.assign(indicator.style, noticeIndicatorStyles(notice.state));
|
|
2294
|
+
element.append(indicator);
|
|
2295
|
+
const copy = container.ownerDocument.createElement("span");
|
|
2296
|
+
copy.textContent = `${notice.message} `;
|
|
2297
|
+
const link = container.ownerDocument.createElement("a");
|
|
2298
|
+
link.href = notice.helpUrl;
|
|
2299
|
+
link.rel = "noopener noreferrer";
|
|
2300
|
+
link.target = "_blank";
|
|
2301
|
+
link.textContent = notice.action;
|
|
2302
|
+
Object.assign(link.style, {
|
|
2303
|
+
color: "inherit",
|
|
2304
|
+
fontWeight: "700",
|
|
2305
|
+
textDecoration: "underline",
|
|
2306
|
+
textDecorationThickness: "1px",
|
|
2307
|
+
textUnderlineOffset: "2px"
|
|
2308
|
+
});
|
|
2309
|
+
copy.append(link);
|
|
2310
|
+
element.append(copy);
|
|
2311
|
+
}
|
|
2312
|
+
});
|
|
2313
|
+
}
|
|
2314
|
+
async function loadTileflowWorldRequest(request, abortController) {
|
|
2315
|
+
const wrapped = new URL(request.url);
|
|
2316
|
+
const channel = wrapped.pathname.slice(1);
|
|
2317
|
+
const registration = wrapped.hostname === "request" ? registrations.get(channel) : void 0;
|
|
2318
|
+
const original = wrapped.searchParams.get("url") ?? "";
|
|
2319
|
+
if (!registration || !isCanonicalWorldTileUrl(original)) {
|
|
2320
|
+
throw new Error("Invalid Tileflow World request bridge");
|
|
2321
|
+
}
|
|
2322
|
+
registration.nextRequestSequence += 1;
|
|
2323
|
+
const requestSequence = registration.nextRequestSequence;
|
|
2324
|
+
const response = await registration.fetch(original, {
|
|
2325
|
+
credentials: "omit",
|
|
2326
|
+
method: "GET",
|
|
2327
|
+
signal: abortController.signal
|
|
2328
|
+
});
|
|
2329
|
+
const registrationStillActive = registrations.get(channel) === registration;
|
|
2330
|
+
const fairUse = parseFairUseState(response.headers.get("Tileflow-Fair-Use"));
|
|
2331
|
+
const ownerNotice = response.headers.get("Tileflow-Fair-Use-Notice") === "owner";
|
|
2332
|
+
if (registrationStillActive && fairUse !== null && requestSequence >= registration.lastNoticeSequence) {
|
|
2333
|
+
if (fairUse === "OPEN") {
|
|
2334
|
+
if (response.ok) {
|
|
2335
|
+
registration.lastNoticeSequence = requestSequence;
|
|
2336
|
+
registration.currentNoticeState = null;
|
|
2337
|
+
registration.onNotice(null);
|
|
2338
|
+
}
|
|
2339
|
+
} else if (fairUse === "MANAGED_REQUIRED" || fairUse === "GRACE" && (ownerNotice || response.status === 429)) {
|
|
2340
|
+
registration.lastNoticeSequence = requestSequence;
|
|
2341
|
+
if (!(registration.currentNoticeState === "MANAGED_REQUIRED" && fairUse === "GRACE")) {
|
|
2342
|
+
registration.currentNoticeState = fairUse;
|
|
2343
|
+
registration.onNotice(
|
|
2344
|
+
Object.freeze({
|
|
2345
|
+
action: ownerAction,
|
|
2346
|
+
helpUrl: parseHelpUrl(response.headers.get("Link")),
|
|
2347
|
+
message: tileflowFairUseNoticeMessage(fairUse),
|
|
2348
|
+
state: fairUse
|
|
2349
|
+
})
|
|
2350
|
+
);
|
|
2351
|
+
}
|
|
2352
|
+
} else if (fairUse === "GRACE" && response.ok) {
|
|
2353
|
+
registration.lastNoticeSequence = requestSequence;
|
|
2354
|
+
if (registration.currentNoticeState !== "MANAGED_REQUIRED") {
|
|
2355
|
+
registration.currentNoticeState = null;
|
|
2356
|
+
registration.onNotice(null);
|
|
2357
|
+
}
|
|
2358
|
+
}
|
|
2359
|
+
}
|
|
2360
|
+
if (response.status === 404 || response.status === 429 && (fairUse === "GRACE" || fairUse === "MANAGED_REQUIRED")) {
|
|
2361
|
+
await cancelResponseBody(response.body);
|
|
2362
|
+
return Object.freeze({
|
|
2363
|
+
cacheControl: "private, no-store",
|
|
2364
|
+
data: new ArrayBuffer(0)
|
|
2365
|
+
});
|
|
2366
|
+
}
|
|
2367
|
+
if (!response.ok) {
|
|
2368
|
+
await cancelResponseBody(response.body);
|
|
2369
|
+
throw new Error(`Tileflow World request failed: ${response.status}`);
|
|
2370
|
+
}
|
|
2371
|
+
return Object.freeze({
|
|
2372
|
+
...response.headers.get("Cache-Control") ? { cacheControl: response.headers.get("Cache-Control") } : {},
|
|
2373
|
+
data: await readWorldTileBody(response, abortController.signal),
|
|
2374
|
+
...response.headers.get("ETag") ? { etag: response.headers.get("ETag") } : {},
|
|
2375
|
+
...response.headers.get("Expires") ? { expires: response.headers.get("Expires") } : {}
|
|
2376
|
+
});
|
|
2377
|
+
}
|
|
2378
|
+
async function readWorldTileBody(response, signal) {
|
|
2379
|
+
if (signal.aborted) {
|
|
2380
|
+
await cancelResponseBody(response.body);
|
|
2381
|
+
throw abortReason(signal);
|
|
2382
|
+
}
|
|
2383
|
+
const declaredLength = parseContentLength(response.headers.get("Content-Length"));
|
|
2384
|
+
if (declaredLength !== null && declaredLength > maximumWorldTileBytes) {
|
|
2385
|
+
await cancelResponseBody(response.body);
|
|
2386
|
+
throw new Error(oversizedWorldTileMessage);
|
|
2387
|
+
}
|
|
2388
|
+
if (!response.body) return new ArrayBuffer(0);
|
|
2389
|
+
const reader = response.body.getReader();
|
|
2390
|
+
const chunks = [];
|
|
2391
|
+
let byteLength = 0;
|
|
2392
|
+
let abortCancellation = null;
|
|
2393
|
+
const cancelForAbort = () => {
|
|
2394
|
+
abortCancellation ??= cancelReader(reader, signal.reason);
|
|
2395
|
+
};
|
|
2396
|
+
signal.addEventListener("abort", cancelForAbort, { once: true });
|
|
2397
|
+
try {
|
|
2398
|
+
while (true) {
|
|
2399
|
+
const { done, value } = await reader.read();
|
|
2400
|
+
throwIfWorldRequestAborted(signal);
|
|
2401
|
+
if (done) break;
|
|
2402
|
+
if (value.byteLength > maximumWorldTileBytes - byteLength) {
|
|
2403
|
+
await cancelReader(reader);
|
|
2404
|
+
throw new Error(oversizedWorldTileMessage);
|
|
2405
|
+
}
|
|
2406
|
+
byteLength += value.byteLength;
|
|
2407
|
+
chunks.push(Uint8Array.from(value));
|
|
2408
|
+
}
|
|
2409
|
+
} catch (error) {
|
|
2410
|
+
if (signal.aborted) {
|
|
2411
|
+
await (abortCancellation ?? cancelReader(reader, signal.reason));
|
|
2412
|
+
throw abortReason(signal);
|
|
2413
|
+
}
|
|
2414
|
+
await cancelReader(reader);
|
|
2415
|
+
if (error instanceof Error && error.message === oversizedWorldTileMessage) throw error;
|
|
2416
|
+
throw new Error(unreadableWorldTileMessage);
|
|
2417
|
+
} finally {
|
|
2418
|
+
signal.removeEventListener("abort", cancelForAbort);
|
|
2419
|
+
reader.releaseLock();
|
|
2420
|
+
}
|
|
2421
|
+
const data = new ArrayBuffer(byteLength);
|
|
2422
|
+
const output = new Uint8Array(data);
|
|
2423
|
+
let offset = 0;
|
|
2424
|
+
for (const chunk of chunks) {
|
|
2425
|
+
output.set(chunk, offset);
|
|
2426
|
+
offset += chunk.byteLength;
|
|
2427
|
+
}
|
|
2428
|
+
return data;
|
|
2429
|
+
}
|
|
2430
|
+
function parseContentLength(value) {
|
|
2431
|
+
const normalized = value?.trim();
|
|
2432
|
+
if (!normalized || !/^\d+$/u.test(normalized)) return null;
|
|
2433
|
+
const length = Number(normalized);
|
|
2434
|
+
return Number.isSafeInteger(length) ? length : maximumWorldTileBytes + 1;
|
|
2435
|
+
}
|
|
2436
|
+
async function cancelResponseBody(body) {
|
|
2437
|
+
if (!body) return;
|
|
2438
|
+
try {
|
|
2439
|
+
await body.cancel();
|
|
2440
|
+
} catch {
|
|
2441
|
+
}
|
|
2442
|
+
}
|
|
2443
|
+
async function cancelReader(reader, reason) {
|
|
2444
|
+
try {
|
|
2445
|
+
await reader.cancel(reason);
|
|
2446
|
+
} catch {
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
function throwIfWorldRequestAborted(signal) {
|
|
2450
|
+
if (signal.aborted) throw abortReason(signal);
|
|
2451
|
+
}
|
|
2452
|
+
function abortReason(signal) {
|
|
2453
|
+
if (signal.reason !== void 0) return signal.reason;
|
|
2454
|
+
return new DOMException("The Tileflow World request was aborted", "AbortError");
|
|
2455
|
+
}
|
|
2456
|
+
function parseFairUseState(value) {
|
|
2457
|
+
if (value === "open") return "OPEN";
|
|
2458
|
+
if (value === "grace") return "GRACE";
|
|
2459
|
+
if (value === "managed-required") return "MANAGED_REQUIRED";
|
|
2460
|
+
return null;
|
|
2461
|
+
}
|
|
2462
|
+
function noticeContainerStyles(state) {
|
|
2463
|
+
const shared2 = {
|
|
2464
|
+
display: "flex",
|
|
2465
|
+
left: "50%",
|
|
2466
|
+
position: "absolute",
|
|
2467
|
+
transform: "translateX(-50%)",
|
|
2468
|
+
zIndex: "3"
|
|
2469
|
+
};
|
|
2470
|
+
if (state === "GRACE") {
|
|
2471
|
+
return {
|
|
2472
|
+
...shared2,
|
|
2473
|
+
alignItems: "center",
|
|
2474
|
+
background: "rgba(252, 250, 244, 0.96)",
|
|
2475
|
+
border: "1px solid rgba(189, 138, 43, 0.28)",
|
|
2476
|
+
borderRadius: "999px",
|
|
2477
|
+
bottom: "24px",
|
|
2478
|
+
boxSizing: "border-box",
|
|
2479
|
+
boxShadow: "0 1px 5px rgba(17, 24, 39, 0.15)",
|
|
2480
|
+
color: "#38433d",
|
|
2481
|
+
font: "560 12px/1.35 system-ui, sans-serif",
|
|
2482
|
+
gap: "7px",
|
|
2483
|
+
maxWidth: "calc(100% - 24px)",
|
|
2484
|
+
padding: "7px 11px 7px 9px",
|
|
2485
|
+
textAlign: "center",
|
|
2486
|
+
width: "max-content"
|
|
2487
|
+
};
|
|
2488
|
+
}
|
|
2489
|
+
return {
|
|
2490
|
+
...shared2,
|
|
2491
|
+
alignItems: "flex-start",
|
|
2492
|
+
background: "rgba(25, 34, 29, 0.96)",
|
|
2493
|
+
border: "1px solid rgba(255, 255, 255, 0.2)",
|
|
2494
|
+
borderRadius: "8px",
|
|
2495
|
+
boxShadow: "0 4px 18px rgba(0, 0, 0, 0.28)",
|
|
2496
|
+
color: "#fff",
|
|
2497
|
+
font: "500 13px/1.45 system-ui, sans-serif",
|
|
2498
|
+
gap: "10px",
|
|
2499
|
+
maxWidth: "min(39rem, calc(100% - 32px))",
|
|
2500
|
+
padding: "12px 14px",
|
|
2501
|
+
textAlign: "left",
|
|
2502
|
+
top: "14px"
|
|
2503
|
+
};
|
|
2504
|
+
}
|
|
2505
|
+
function noticeIndicatorStyles(state) {
|
|
2506
|
+
if (state === "GRACE") {
|
|
2507
|
+
return {
|
|
2508
|
+
background: "#c58c28",
|
|
2509
|
+
borderRadius: "999px",
|
|
2510
|
+
flex: "0 0 auto",
|
|
2511
|
+
height: "7px",
|
|
2512
|
+
width: "7px"
|
|
2513
|
+
};
|
|
2514
|
+
}
|
|
2515
|
+
return {
|
|
2516
|
+
alignItems: "center",
|
|
2517
|
+
background: "#d69e39",
|
|
2518
|
+
borderRadius: "999px",
|
|
2519
|
+
color: "#17201b",
|
|
2520
|
+
display: "inline-flex",
|
|
2521
|
+
flex: "0 0 auto",
|
|
2522
|
+
font: "800 12px/1 system-ui, sans-serif",
|
|
2523
|
+
height: "18px",
|
|
2524
|
+
justifyContent: "center",
|
|
2525
|
+
marginTop: "1px",
|
|
2526
|
+
width: "18px"
|
|
2527
|
+
};
|
|
2528
|
+
}
|
|
2529
|
+
function parseHelpUrl(value) {
|
|
2530
|
+
const match = value?.match(/<([^>]+)>\s*;\s*rel=(?:"help"|help)(?:\s*;|\s*$)/iu);
|
|
2531
|
+
if (!match?.[1]) return defaultHelpUrl;
|
|
2532
|
+
try {
|
|
2533
|
+
const url = new URL(match[1]);
|
|
2534
|
+
if (url.protocol !== "https:" || url.username || url.password || url.hostname !== "tileflow.dev" && !url.hostname.endsWith(".tileflow.dev")) {
|
|
2535
|
+
return defaultHelpUrl;
|
|
2536
|
+
}
|
|
2537
|
+
return url.toString();
|
|
2538
|
+
} catch {
|
|
2539
|
+
return defaultHelpUrl;
|
|
2540
|
+
}
|
|
2541
|
+
}
|
|
2542
|
+
function isCanonicalWorldTileUrl(value) {
|
|
2543
|
+
try {
|
|
2544
|
+
const url = new URL(value);
|
|
2545
|
+
const exactPath = url.pathname.match(
|
|
2546
|
+
/^\/tiles\/world\/([^/]+)\/(?:0|[1-9]\d*)\/(?:0|[1-9]\d*)\/(?:0|[1-9]\d*)\.pbf$/u
|
|
2547
|
+
);
|
|
2548
|
+
if (url.protocol !== "https:" || url.hostname !== "tileflow.dev" && !url.hostname.endsWith(".tileflow.dev") || url.port || url.username || url.password || url.hash || !exactPath || !isTileflowWorldReleaseId(exactPath[1])) {
|
|
2549
|
+
return false;
|
|
2550
|
+
}
|
|
2551
|
+
const allowedQueryKeys = /* @__PURE__ */ new Set([
|
|
2552
|
+
"grant",
|
|
2553
|
+
"map",
|
|
2554
|
+
"renderExpires",
|
|
2555
|
+
"renderSignature",
|
|
2556
|
+
"session",
|
|
2557
|
+
"styleId",
|
|
2558
|
+
"worldDescriptorSha256"
|
|
2559
|
+
]);
|
|
2560
|
+
if ([...url.searchParams.keys()].some((key) => !allowedQueryKeys.has(key))) return false;
|
|
2561
|
+
const descriptorDigests = url.searchParams.getAll("worldDescriptorSha256");
|
|
2562
|
+
return descriptorDigests.length === 1 && /^[0-9a-f]{64}$/u.test(descriptorDigests[0] ?? "");
|
|
2563
|
+
} catch {
|
|
2564
|
+
return false;
|
|
2565
|
+
}
|
|
2566
|
+
}
|
|
2567
|
+
function createChannel() {
|
|
2568
|
+
channelSequence += 1;
|
|
2569
|
+
if (typeof crypto !== "undefined" && "randomUUID" in crypto) {
|
|
2570
|
+
return `bridge_${crypto.randomUUID().replaceAll("-", "_")}`;
|
|
2571
|
+
}
|
|
2572
|
+
return `bridge_${Date.now().toString(36)}_${channelSequence.toString(36)}`;
|
|
2573
|
+
}
|
|
2574
|
+
|
|
2575
|
+
// src/terrain/contour-protocol.ts
|
|
2576
|
+
var tileflowContourProtocol = "tileflow-contour";
|
|
2577
|
+
var tileflowContourSourceLayer = "contours";
|
|
2578
|
+
function minimumTileflowContourSourceInterval(zoom) {
|
|
2579
|
+
if (zoom <= 4) return 250;
|
|
2580
|
+
if (zoom <= 7) return 100;
|
|
2581
|
+
if (zoom <= 10) return 50;
|
|
2582
|
+
if (zoom <= 12) return 20;
|
|
2583
|
+
return 10;
|
|
2584
|
+
}
|
|
2585
|
+
function isTileflowContourDensityWithinBudget(minorInterval, multiplier, zoom) {
|
|
2586
|
+
const minimumScaledInterval = minimumTileflowContourSourceInterval(zoom) * multiplier;
|
|
2587
|
+
const tolerance = Number.EPSILON * Math.max(1, minorInterval, minimumScaledInterval) * 8;
|
|
2588
|
+
return minorInterval + tolerance >= minimumScaledInterval;
|
|
2589
|
+
}
|
|
2590
|
+
var maximumDemUrlLength = 2048;
|
|
2591
|
+
var maximumProtocolUrlLength = 8192;
|
|
2592
|
+
var contourTilePathPattern = /^\/(\d{1,2})\/(\d{1,8})\/(\d{1,8})\.pbf$/u;
|
|
2593
|
+
var allowedParameters = /* @__PURE__ */ new Set([
|
|
2594
|
+
"demMaxzoom",
|
|
2595
|
+
"demUrl",
|
|
2596
|
+
"encoding",
|
|
2597
|
+
"maxzoom",
|
|
2598
|
+
"multiplier",
|
|
2599
|
+
"overzoom",
|
|
2600
|
+
"thresholds"
|
|
2601
|
+
]);
|
|
2602
|
+
function parseTileflowContourProtocolRequest(value) {
|
|
2603
|
+
if (value.length > maximumProtocolUrlLength || /[\\\p{Cc}]/u.test(value)) {
|
|
2604
|
+
throw new Error("Invalid Tileflow contour protocol URL.");
|
|
2605
|
+
}
|
|
2606
|
+
let url;
|
|
2607
|
+
try {
|
|
2608
|
+
url = new URL(value);
|
|
2609
|
+
} catch {
|
|
2610
|
+
throw new Error("Invalid Tileflow contour protocol URL.");
|
|
2611
|
+
}
|
|
2612
|
+
if (url.protocol !== `${tileflowContourProtocol}:` || url.hostname !== "tiles" || url.username || url.password || url.hash) {
|
|
2613
|
+
throw new Error("Invalid Tileflow contour protocol URL.");
|
|
2614
|
+
}
|
|
2615
|
+
for (const key of url.searchParams.keys()) {
|
|
2616
|
+
if (!allowedParameters.has(key) || url.searchParams.getAll(key).length !== 1) {
|
|
2617
|
+
throw new Error("Invalid Tileflow contour protocol URL parameters.");
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
if ([...allowedParameters].some((key) => !url.searchParams.has(key))) {
|
|
2621
|
+
throw new Error("Incomplete Tileflow contour protocol URL parameters.");
|
|
2622
|
+
}
|
|
2623
|
+
const tile = contourTilePathPattern.exec(url.pathname);
|
|
2624
|
+
if (!tile) throw new Error("Invalid Tileflow contour tile coordinates.");
|
|
2625
|
+
const z = Number(tile[1]);
|
|
2626
|
+
const x3 = Number(tile[2]);
|
|
2627
|
+
const y = Number(tile[3]);
|
|
2628
|
+
const config = {
|
|
2629
|
+
demMaxzoom: parseBoundedInteger(url.searchParams.get("demMaxzoom"), 0, 24),
|
|
2630
|
+
demUrl: url.searchParams.get("demUrl") ?? "",
|
|
2631
|
+
encoding: parseEncoding(url.searchParams.get("encoding")),
|
|
2632
|
+
maxzoom: parseBoundedInteger(url.searchParams.get("maxzoom"), 0, 24),
|
|
2633
|
+
multiplier: parseBoundedNumber(url.searchParams.get("multiplier"), 1e-3, 100),
|
|
2634
|
+
overzoom: parseBoundedInteger(url.searchParams.get("overzoom"), 0, 8),
|
|
2635
|
+
thresholds: parseThresholds(url.searchParams.get("thresholds") ?? "")
|
|
2636
|
+
};
|
|
2637
|
+
assertTileflowContourProtocolConfig(config);
|
|
2638
|
+
const axisLimit = 2 ** z;
|
|
2639
|
+
const minimumThresholdZoom = Math.min(...Object.keys(config.thresholds).map(Number));
|
|
2640
|
+
if (z < minimumThresholdZoom || z > config.maxzoom || x3 >= axisLimit || y >= axisLimit) {
|
|
2641
|
+
throw new Error("Invalid Tileflow contour tile coordinates.");
|
|
2642
|
+
}
|
|
2643
|
+
return { config, x: x3, y, z };
|
|
2644
|
+
}
|
|
2645
|
+
function isSafeTileflowDemUrlTemplate(value) {
|
|
2646
|
+
if (value.length === 0 || value.length > maximumDemUrlLength || value.startsWith("//") || value.includes("#") || /[\\\p{Cc}]/u.test(value)) {
|
|
2647
|
+
return false;
|
|
2648
|
+
}
|
|
2649
|
+
for (const placeholder of ["{z}", "{x}", "{y}"]) {
|
|
2650
|
+
if (value.split(placeholder).length !== 2) return false;
|
|
2651
|
+
}
|
|
2652
|
+
if (/\{[^{}]*\}/u.test(value.replaceAll("{z}", "").replaceAll("{x}", "").replaceAll("{y}", ""))) {
|
|
2653
|
+
return false;
|
|
2654
|
+
}
|
|
2655
|
+
try {
|
|
2656
|
+
const url = new URL(value.replaceAll("{z}", "0").replaceAll("{x}", "0").replaceAll("{y}", "0"));
|
|
2657
|
+
return (url.protocol === "https:" || url.protocol === "http:" && isLoopbackHostname(url.hostname)) && !url.username && !url.password && !url.hash;
|
|
2658
|
+
} catch {
|
|
2659
|
+
return false;
|
|
2660
|
+
}
|
|
2661
|
+
}
|
|
2662
|
+
function assertTileflowContourProtocolConfig(config) {
|
|
2663
|
+
if (!isSafeTileflowDemUrlTemplate(config.demUrl)) {
|
|
2664
|
+
throw new Error(
|
|
2665
|
+
"Tileflow contour demUrl must be a safe HTTP(S) template containing {z}, {x}, and {y}."
|
|
2666
|
+
);
|
|
2667
|
+
}
|
|
2668
|
+
if (config.encoding !== "mapbox" && config.encoding !== "terrarium") {
|
|
2669
|
+
throw new Error("Tileflow contour encoding must be mapbox or terrarium.");
|
|
2670
|
+
}
|
|
2671
|
+
assertBoundedInteger(config.demMaxzoom, 0, 24, "demMaxzoom");
|
|
2672
|
+
assertBoundedInteger(config.maxzoom, 0, 24, "maxzoom");
|
|
2673
|
+
assertBoundedInteger(config.overzoom, 0, 8, "overzoom");
|
|
2674
|
+
assertBoundedNumber(config.multiplier, 1e-3, 100, "multiplier");
|
|
2675
|
+
const entries = Object.entries(config.thresholds);
|
|
2676
|
+
if (entries.length === 0 || entries.length > 25) {
|
|
2677
|
+
throw new Error("Tileflow contour thresholds must contain between 1 and 25 zoom entries.");
|
|
2678
|
+
}
|
|
2679
|
+
for (const [zoom, pair] of entries) {
|
|
2680
|
+
if (!/^(?:0|[1-9]\d?)$/u.test(zoom)) {
|
|
2681
|
+
throw new Error("Tileflow contour threshold zooms must be integers from 0 to 24.");
|
|
2682
|
+
}
|
|
2683
|
+
assertBoundedInteger(Number(zoom), 0, 24, "threshold zoom");
|
|
2684
|
+
if (!Array.isArray(pair) || pair.length !== 2) {
|
|
2685
|
+
throw new Error("Tileflow contour thresholds must be [minor, index] pairs.");
|
|
2686
|
+
}
|
|
2687
|
+
const [minor, index] = pair;
|
|
2688
|
+
assertBoundedNumber(minor, 1e-3, 1e5, "minor threshold");
|
|
2689
|
+
assertBoundedNumber(index, minor, 1e5, "index threshold");
|
|
2690
|
+
if (!isTileflowContourDensityWithinBudget(minor, config.multiplier, Number(zoom))) {
|
|
2691
|
+
throw new Error("Tileflow contour threshold exceeds the supported density budget.");
|
|
2692
|
+
}
|
|
2693
|
+
if (!isWholeContourMultiple(index, minor)) {
|
|
2694
|
+
throw new Error(
|
|
2695
|
+
"Tileflow contour index threshold must be a whole multiple of the minor threshold."
|
|
2696
|
+
);
|
|
2697
|
+
}
|
|
2698
|
+
}
|
|
2699
|
+
const thresholdZooms = entries.map(([zoom]) => Number(zoom));
|
|
2700
|
+
if (Math.max(...thresholdZooms) > config.maxzoom) {
|
|
2701
|
+
throw new Error("Tileflow contour maxzoom must include every threshold zoom.");
|
|
2702
|
+
}
|
|
2703
|
+
if (config.overzoom > Math.min(...thresholdZooms)) {
|
|
2704
|
+
throw new Error("Tileflow contour overzoom must not produce a negative DEM zoom.");
|
|
2705
|
+
}
|
|
2706
|
+
}
|
|
2707
|
+
function parseThresholds(value) {
|
|
2708
|
+
if (!value || value.length > 1024) {
|
|
2709
|
+
throw new Error("Invalid Tileflow contour thresholds.");
|
|
2710
|
+
}
|
|
2711
|
+
const result = {};
|
|
2712
|
+
for (const entry of value.split(";")) {
|
|
2713
|
+
const match = /^(0|[1-9]\d?):([^,]+),([^,]+)$/u.exec(entry);
|
|
2714
|
+
if (!match) throw new Error("Invalid Tileflow contour thresholds.");
|
|
2715
|
+
const zoom = parseBoundedInteger(match[1], 0, 24);
|
|
2716
|
+
if (Object.hasOwn(result, zoom)) throw new Error("Duplicate Tileflow contour threshold zoom.");
|
|
2717
|
+
result[zoom] = [
|
|
2718
|
+
parseBoundedNumber(match[2], 1e-3, 1e5),
|
|
2719
|
+
parseBoundedNumber(match[3], 1e-3, 1e5)
|
|
2720
|
+
];
|
|
2721
|
+
}
|
|
2722
|
+
return result;
|
|
2723
|
+
}
|
|
2724
|
+
function parseEncoding(value) {
|
|
2725
|
+
if (value !== "mapbox" && value !== "terrarium") {
|
|
2726
|
+
throw new Error("Invalid Tileflow contour encoding.");
|
|
2727
|
+
}
|
|
2728
|
+
return value;
|
|
2729
|
+
}
|
|
2730
|
+
function parseBoundedInteger(value, minimum, maximum) {
|
|
2731
|
+
if (value === null || !/^(?:0|[1-9]\d*)$/u.test(value)) {
|
|
2732
|
+
throw new Error("Invalid Tileflow contour integer.");
|
|
2733
|
+
}
|
|
2734
|
+
const parsed = Number(value);
|
|
2735
|
+
assertBoundedInteger(parsed, minimum, maximum, "integer");
|
|
2736
|
+
return parsed;
|
|
2737
|
+
}
|
|
2738
|
+
function parseBoundedNumber(value, minimum, maximum) {
|
|
2739
|
+
if (value === null || value.trim() !== value || value === "") {
|
|
2740
|
+
throw new Error("Invalid Tileflow contour number.");
|
|
2741
|
+
}
|
|
2742
|
+
const parsed = Number(value);
|
|
2743
|
+
assertBoundedNumber(parsed, minimum, maximum, "number");
|
|
2744
|
+
return parsed;
|
|
2745
|
+
}
|
|
2746
|
+
function assertBoundedInteger(value, minimum, maximum, name) {
|
|
2747
|
+
if (!Number.isInteger(value) || value < minimum || value > maximum) {
|
|
2748
|
+
throw new Error(`Tileflow contour ${name} is outside its supported range.`);
|
|
2749
|
+
}
|
|
2750
|
+
}
|
|
2751
|
+
function assertBoundedNumber(value, minimum, maximum, name) {
|
|
2752
|
+
if (!Number.isFinite(value) || value < minimum || value > maximum) {
|
|
2753
|
+
throw new Error(`Tileflow contour ${name} is outside its supported range.`);
|
|
2754
|
+
}
|
|
2755
|
+
}
|
|
2756
|
+
function isWholeContourMultiple(value, interval) {
|
|
2757
|
+
const ratio = value / interval;
|
|
2758
|
+
return Math.abs(ratio - Math.round(ratio)) <= Number.EPSILON * Math.max(1, ratio) * 8;
|
|
2759
|
+
}
|
|
2760
|
+
function isLoopbackHostname(hostname) {
|
|
2761
|
+
const normalized = hostname.toLowerCase().replace(/^\[|\]$/gu, "");
|
|
2762
|
+
return normalized === "localhost" || normalized === "::1" || normalized === "0:0:0:0:0:0:0:1" || /^127(?:\.\d{1,3}){3}$/u.test(normalized);
|
|
2763
|
+
}
|
|
2764
|
+
|
|
2765
|
+
// src/contour-browser.ts
|
|
2766
|
+
var tileflowMaplibreContourVersion = "0.1.0";
|
|
2767
|
+
var registeredContourProtocols = /* @__PURE__ */ new WeakSet();
|
|
2768
|
+
function registerTileflowContourProtocol(registry, options = {}) {
|
|
2769
|
+
if (registeredContourProtocols.has(registry.addProtocol)) return;
|
|
2770
|
+
const handler = createTileflowContourProtocolHandler(
|
|
2771
|
+
options.loadMaplibreContour ?? loadPinnedMaplibreContour
|
|
2772
|
+
);
|
|
2773
|
+
registry.addProtocol(tileflowContourProtocol, handler);
|
|
2774
|
+
registeredContourProtocols.add(registry.addProtocol);
|
|
2775
|
+
}
|
|
2776
|
+
function createTileflowContourProtocolHandler(loadModule) {
|
|
2777
|
+
let library;
|
|
2778
|
+
const sources = /* @__PURE__ */ new Map();
|
|
2779
|
+
return async (request, abortController) => {
|
|
2780
|
+
if (abortController.signal.aborted) throw new Error("Tileflow contour request aborted.");
|
|
2781
|
+
const { config, x: x3, y, z } = parseTileflowContourProtocolRequest(request.url);
|
|
2782
|
+
const sourceKey = JSON.stringify([config.demUrl, config.encoding, config.demMaxzoom]);
|
|
2783
|
+
let source = sources.get(sourceKey);
|
|
2784
|
+
if (!source) {
|
|
2785
|
+
if (sources.size >= 16) sources.delete(sources.keys().next().value);
|
|
2786
|
+
library ??= loadModule().then(resolveMaplibreContourLibrary);
|
|
2787
|
+
source = library.then(
|
|
2788
|
+
(module) => new module.DemSource({
|
|
2789
|
+
encoding: config.encoding,
|
|
2790
|
+
maxzoom: config.demMaxzoom,
|
|
2791
|
+
url: config.demUrl,
|
|
2792
|
+
// Upstream workers use a generated `blob:` URL. The main-thread manager keeps the
|
|
2793
|
+
// protocol usable in strict-CSP applications and deterministic local captures.
|
|
2794
|
+
worker: false
|
|
2795
|
+
})
|
|
2796
|
+
);
|
|
2797
|
+
source = source.catch((error) => {
|
|
2798
|
+
sources.delete(sourceKey);
|
|
2799
|
+
library = void 0;
|
|
2800
|
+
throw error;
|
|
2801
|
+
});
|
|
2802
|
+
sources.set(sourceKey, source);
|
|
2803
|
+
}
|
|
2804
|
+
const contourSource = await source;
|
|
2805
|
+
if (abortController.signal.aborted) throw new Error("Tileflow contour request aborted.");
|
|
2806
|
+
const contourUrl = contourSource.contourProtocolUrl({
|
|
2807
|
+
contourLayer: tileflowContourSourceLayer,
|
|
2808
|
+
elevationKey: "ele",
|
|
2809
|
+
levelKey: "level",
|
|
2810
|
+
multiplier: config.multiplier,
|
|
2811
|
+
overzoom: config.overzoom,
|
|
2812
|
+
thresholds: mutableThresholds(config.thresholds)
|
|
2813
|
+
}).replace("{z}", String(z)).replace("{x}", String(x3)).replace("{y}", String(y));
|
|
2814
|
+
return contourSource.contourProtocolV4({ url: contourUrl }, abortController);
|
|
2815
|
+
};
|
|
2816
|
+
}
|
|
2817
|
+
async function loadPinnedMaplibreContour() {
|
|
2818
|
+
return Promise.resolve().then(() => (init_dist(), dist_exports));
|
|
2819
|
+
}
|
|
2820
|
+
function resolveMaplibreContourLibrary(module) {
|
|
2821
|
+
if (!module || typeof module !== "object") {
|
|
2822
|
+
throw new Error("maplibre-contour 0.1.0 did not expose its browser module.");
|
|
2823
|
+
}
|
|
2824
|
+
const library = module.default;
|
|
2825
|
+
if (!library || typeof library !== "object" || typeof library.DemSource !== "function") {
|
|
2826
|
+
throw new Error("maplibre-contour 0.1.0 did not expose DemSource.");
|
|
2827
|
+
}
|
|
2828
|
+
return library;
|
|
2829
|
+
}
|
|
2830
|
+
function mutableThresholds(thresholds) {
|
|
2831
|
+
return Object.fromEntries(
|
|
2832
|
+
Object.entries(thresholds).map(([zoom, [minor, index]]) => [Number(zoom), [minor, index]])
|
|
2833
|
+
);
|
|
2834
|
+
}
|
|
2835
|
+
|
|
2836
|
+
// ../../node_modules/.pnpm/fflate@0.8.2/node_modules/fflate/esm/browser.js
|
|
2837
|
+
var u8 = Uint8Array;
|
|
2838
|
+
var u16 = Uint16Array;
|
|
2839
|
+
var i32 = Int32Array;
|
|
2840
|
+
var fleb = new u8([
|
|
2841
|
+
0,
|
|
2842
|
+
0,
|
|
2843
|
+
0,
|
|
2844
|
+
0,
|
|
2845
|
+
0,
|
|
2846
|
+
0,
|
|
2847
|
+
0,
|
|
2848
|
+
0,
|
|
2849
|
+
1,
|
|
2850
|
+
1,
|
|
2851
|
+
1,
|
|
2852
|
+
1,
|
|
2853
|
+
2,
|
|
2854
|
+
2,
|
|
2855
|
+
2,
|
|
2856
|
+
2,
|
|
2857
|
+
3,
|
|
2858
|
+
3,
|
|
2859
|
+
3,
|
|
2860
|
+
3,
|
|
2861
|
+
4,
|
|
2862
|
+
4,
|
|
2863
|
+
4,
|
|
2864
|
+
4,
|
|
2865
|
+
5,
|
|
2866
|
+
5,
|
|
2867
|
+
5,
|
|
2868
|
+
5,
|
|
2869
|
+
0,
|
|
2870
|
+
/* unused */
|
|
2871
|
+
0,
|
|
2872
|
+
0,
|
|
2873
|
+
/* impossible */
|
|
2874
|
+
0
|
|
2875
|
+
]);
|
|
2876
|
+
var fdeb = new u8([
|
|
2877
|
+
0,
|
|
2878
|
+
0,
|
|
2879
|
+
0,
|
|
2880
|
+
0,
|
|
2881
|
+
1,
|
|
2882
|
+
1,
|
|
2883
|
+
2,
|
|
2884
|
+
2,
|
|
2885
|
+
3,
|
|
2886
|
+
3,
|
|
2887
|
+
4,
|
|
2888
|
+
4,
|
|
2889
|
+
5,
|
|
2890
|
+
5,
|
|
2891
|
+
6,
|
|
2892
|
+
6,
|
|
2893
|
+
7,
|
|
2894
|
+
7,
|
|
2895
|
+
8,
|
|
2896
|
+
8,
|
|
2897
|
+
9,
|
|
2898
|
+
9,
|
|
2899
|
+
10,
|
|
2900
|
+
10,
|
|
2901
|
+
11,
|
|
2902
|
+
11,
|
|
2903
|
+
12,
|
|
2904
|
+
12,
|
|
2905
|
+
13,
|
|
2906
|
+
13,
|
|
2907
|
+
/* unused */
|
|
2908
|
+
0,
|
|
2909
|
+
0
|
|
2910
|
+
]);
|
|
2911
|
+
var clim = new u8([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
|
|
2912
|
+
var freb = function(eb, start) {
|
|
2913
|
+
var b2 = new u16(31);
|
|
2914
|
+
for (var i2 = 0; i2 < 31; ++i2) {
|
|
2915
|
+
b2[i2] = start += 1 << eb[i2 - 1];
|
|
2916
|
+
}
|
|
2917
|
+
var r = new i32(b2[30]);
|
|
2918
|
+
for (var i2 = 1; i2 < 30; ++i2) {
|
|
2919
|
+
for (var j2 = b2[i2]; j2 < b2[i2 + 1]; ++j2) {
|
|
2920
|
+
r[j2] = j2 - b2[i2] << 5 | i2;
|
|
2921
|
+
}
|
|
2922
|
+
}
|
|
2923
|
+
return { b: b2, r };
|
|
2924
|
+
};
|
|
2925
|
+
var _a = freb(fleb, 2);
|
|
2926
|
+
var fl = _a.b;
|
|
2927
|
+
var revfl = _a.r;
|
|
2928
|
+
fl[28] = 258, revfl[258] = 28;
|
|
2929
|
+
var _b = freb(fdeb, 0);
|
|
2930
|
+
var fd = _b.b;
|
|
2931
|
+
var revfd = _b.r;
|
|
2932
|
+
var rev = new u16(32768);
|
|
2933
|
+
for (i = 0; i < 32768; ++i) {
|
|
2934
|
+
x = (i & 43690) >> 1 | (i & 21845) << 1;
|
|
2935
|
+
x = (x & 52428) >> 2 | (x & 13107) << 2;
|
|
2936
|
+
x = (x & 61680) >> 4 | (x & 3855) << 4;
|
|
2937
|
+
rev[i] = ((x & 65280) >> 8 | (x & 255) << 8) >> 1;
|
|
2938
|
+
}
|
|
2939
|
+
var x;
|
|
2940
|
+
var i;
|
|
2941
|
+
var hMap = (function(cd, mb, r) {
|
|
2942
|
+
var s = cd.length;
|
|
2943
|
+
var i2 = 0;
|
|
2944
|
+
var l2 = new u16(mb);
|
|
2945
|
+
for (; i2 < s; ++i2) {
|
|
2946
|
+
if (cd[i2])
|
|
2947
|
+
++l2[cd[i2] - 1];
|
|
2948
|
+
}
|
|
2949
|
+
var le = new u16(mb);
|
|
2950
|
+
for (i2 = 1; i2 < mb; ++i2) {
|
|
2951
|
+
le[i2] = le[i2 - 1] + l2[i2 - 1] << 1;
|
|
2952
|
+
}
|
|
2953
|
+
var co;
|
|
2954
|
+
if (r) {
|
|
2955
|
+
co = new u16(1 << mb);
|
|
2956
|
+
var rvb = 15 - mb;
|
|
2957
|
+
for (i2 = 0; i2 < s; ++i2) {
|
|
2958
|
+
if (cd[i2]) {
|
|
2959
|
+
var sv = i2 << 4 | cd[i2];
|
|
2960
|
+
var r_1 = mb - cd[i2];
|
|
2961
|
+
var v2 = le[cd[i2] - 1]++ << r_1;
|
|
2962
|
+
for (var m2 = v2 | (1 << r_1) - 1; v2 <= m2; ++v2) {
|
|
2963
|
+
co[rev[v2] >> rvb] = sv;
|
|
2964
|
+
}
|
|
2965
|
+
}
|
|
2966
|
+
}
|
|
2967
|
+
} else {
|
|
2968
|
+
co = new u16(s);
|
|
2969
|
+
for (i2 = 0; i2 < s; ++i2) {
|
|
2970
|
+
if (cd[i2]) {
|
|
2971
|
+
co[i2] = rev[le[cd[i2] - 1]++] >> 15 - cd[i2];
|
|
2972
|
+
}
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
return co;
|
|
2976
|
+
});
|
|
2977
|
+
var flt = new u8(288);
|
|
2978
|
+
for (i = 0; i < 144; ++i)
|
|
2979
|
+
flt[i] = 8;
|
|
2980
|
+
var i;
|
|
2981
|
+
for (i = 144; i < 256; ++i)
|
|
2982
|
+
flt[i] = 9;
|
|
2983
|
+
var i;
|
|
2984
|
+
for (i = 256; i < 280; ++i)
|
|
2985
|
+
flt[i] = 7;
|
|
2986
|
+
var i;
|
|
2987
|
+
for (i = 280; i < 288; ++i)
|
|
2988
|
+
flt[i] = 8;
|
|
2989
|
+
var i;
|
|
2990
|
+
var fdt = new u8(32);
|
|
2991
|
+
for (i = 0; i < 32; ++i)
|
|
2992
|
+
fdt[i] = 5;
|
|
2993
|
+
var i;
|
|
2994
|
+
var flrm = /* @__PURE__ */ hMap(flt, 9, 1);
|
|
2995
|
+
var fdrm = /* @__PURE__ */ hMap(fdt, 5, 1);
|
|
2996
|
+
var max = function(a) {
|
|
2997
|
+
var m2 = a[0];
|
|
2998
|
+
for (var i2 = 1; i2 < a.length; ++i2) {
|
|
2999
|
+
if (a[i2] > m2)
|
|
3000
|
+
m2 = a[i2];
|
|
3001
|
+
}
|
|
3002
|
+
return m2;
|
|
3003
|
+
};
|
|
3004
|
+
var bits = function(d, p, m2) {
|
|
3005
|
+
var o = p / 8 | 0;
|
|
3006
|
+
return (d[o] | d[o + 1] << 8) >> (p & 7) & m2;
|
|
3007
|
+
};
|
|
3008
|
+
var bits16 = function(d, p) {
|
|
3009
|
+
var o = p / 8 | 0;
|
|
3010
|
+
return (d[o] | d[o + 1] << 8 | d[o + 2] << 16) >> (p & 7);
|
|
3011
|
+
};
|
|
3012
|
+
var shft = function(p) {
|
|
3013
|
+
return (p + 7) / 8 | 0;
|
|
3014
|
+
};
|
|
3015
|
+
var slc = function(v2, s, e) {
|
|
3016
|
+
if (s == null || s < 0)
|
|
3017
|
+
s = 0;
|
|
3018
|
+
if (e == null || e > v2.length)
|
|
3019
|
+
e = v2.length;
|
|
3020
|
+
return new u8(v2.subarray(s, e));
|
|
3021
|
+
};
|
|
3022
|
+
var ec = [
|
|
3023
|
+
"unexpected EOF",
|
|
3024
|
+
"invalid block type",
|
|
3025
|
+
"invalid length/literal",
|
|
3026
|
+
"invalid distance",
|
|
3027
|
+
"stream finished",
|
|
3028
|
+
"no stream handler",
|
|
3029
|
+
,
|
|
3030
|
+
"no callback",
|
|
3031
|
+
"invalid UTF-8 data",
|
|
3032
|
+
"extra field too long",
|
|
3033
|
+
"date not in range 1980-2099",
|
|
3034
|
+
"filename too long",
|
|
3035
|
+
"stream finishing",
|
|
3036
|
+
"invalid zip data"
|
|
3037
|
+
// determined by unknown compression method
|
|
3038
|
+
];
|
|
3039
|
+
var err = function(ind, msg, nt) {
|
|
3040
|
+
var e = new Error(msg || ec[ind]);
|
|
3041
|
+
e.code = ind;
|
|
3042
|
+
if (Error.captureStackTrace)
|
|
3043
|
+
Error.captureStackTrace(e, err);
|
|
3044
|
+
if (!nt)
|
|
3045
|
+
throw e;
|
|
3046
|
+
return e;
|
|
3047
|
+
};
|
|
3048
|
+
var inflt = function(dat, st, buf, dict) {
|
|
3049
|
+
var sl = dat.length, dl = dict ? dict.length : 0;
|
|
3050
|
+
if (!sl || st.f && !st.l)
|
|
3051
|
+
return buf || new u8(0);
|
|
3052
|
+
var noBuf = !buf;
|
|
3053
|
+
var resize = noBuf || st.i != 2;
|
|
3054
|
+
var noSt = st.i;
|
|
3055
|
+
if (noBuf)
|
|
3056
|
+
buf = new u8(sl * 3);
|
|
3057
|
+
var cbuf = function(l3) {
|
|
3058
|
+
var bl = buf.length;
|
|
3059
|
+
if (l3 > bl) {
|
|
3060
|
+
var nbuf = new u8(Math.max(bl * 2, l3));
|
|
3061
|
+
nbuf.set(buf);
|
|
3062
|
+
buf = nbuf;
|
|
3063
|
+
}
|
|
3064
|
+
};
|
|
3065
|
+
var final = st.f || 0, pos = st.p || 0, bt = st.b || 0, lm = st.l, dm = st.d, lbt = st.m, dbt = st.n;
|
|
3066
|
+
var tbts = sl * 8;
|
|
3067
|
+
do {
|
|
3068
|
+
if (!lm) {
|
|
3069
|
+
final = bits(dat, pos, 1);
|
|
3070
|
+
var type = bits(dat, pos + 1, 3);
|
|
3071
|
+
pos += 3;
|
|
3072
|
+
if (!type) {
|
|
3073
|
+
var s = shft(pos) + 4, l2 = dat[s - 4] | dat[s - 3] << 8, t = s + l2;
|
|
3074
|
+
if (t > sl) {
|
|
3075
|
+
if (noSt)
|
|
3076
|
+
err(0);
|
|
3077
|
+
break;
|
|
3078
|
+
}
|
|
3079
|
+
if (resize)
|
|
3080
|
+
cbuf(bt + l2);
|
|
3081
|
+
buf.set(dat.subarray(s, t), bt);
|
|
3082
|
+
st.b = bt += l2, st.p = pos = t * 8, st.f = final;
|
|
3083
|
+
continue;
|
|
3084
|
+
} else if (type == 1)
|
|
3085
|
+
lm = flrm, dm = fdrm, lbt = 9, dbt = 5;
|
|
3086
|
+
else if (type == 2) {
|
|
3087
|
+
var hLit = bits(dat, pos, 31) + 257, hcLen = bits(dat, pos + 10, 15) + 4;
|
|
3088
|
+
var tl = hLit + bits(dat, pos + 5, 31) + 1;
|
|
3089
|
+
pos += 14;
|
|
3090
|
+
var ldt = new u8(tl);
|
|
3091
|
+
var clt = new u8(19);
|
|
3092
|
+
for (var i2 = 0; i2 < hcLen; ++i2) {
|
|
3093
|
+
clt[clim[i2]] = bits(dat, pos + i2 * 3, 7);
|
|
3094
|
+
}
|
|
3095
|
+
pos += hcLen * 3;
|
|
3096
|
+
var clb = max(clt), clbmsk = (1 << clb) - 1;
|
|
3097
|
+
var clm = hMap(clt, clb, 1);
|
|
3098
|
+
for (var i2 = 0; i2 < tl; ) {
|
|
3099
|
+
var r = clm[bits(dat, pos, clbmsk)];
|
|
3100
|
+
pos += r & 15;
|
|
3101
|
+
var s = r >> 4;
|
|
3102
|
+
if (s < 16) {
|
|
3103
|
+
ldt[i2++] = s;
|
|
3104
|
+
} else {
|
|
3105
|
+
var c = 0, n = 0;
|
|
3106
|
+
if (s == 16)
|
|
3107
|
+
n = 3 + bits(dat, pos, 3), pos += 2, c = ldt[i2 - 1];
|
|
3108
|
+
else if (s == 17)
|
|
3109
|
+
n = 3 + bits(dat, pos, 7), pos += 3;
|
|
3110
|
+
else if (s == 18)
|
|
3111
|
+
n = 11 + bits(dat, pos, 127), pos += 7;
|
|
3112
|
+
while (n--)
|
|
3113
|
+
ldt[i2++] = c;
|
|
3114
|
+
}
|
|
3115
|
+
}
|
|
3116
|
+
var lt = ldt.subarray(0, hLit), dt = ldt.subarray(hLit);
|
|
3117
|
+
lbt = max(lt);
|
|
3118
|
+
dbt = max(dt);
|
|
3119
|
+
lm = hMap(lt, lbt, 1);
|
|
3120
|
+
dm = hMap(dt, dbt, 1);
|
|
3121
|
+
} else
|
|
3122
|
+
err(1);
|
|
3123
|
+
if (pos > tbts) {
|
|
3124
|
+
if (noSt)
|
|
3125
|
+
err(0);
|
|
3126
|
+
break;
|
|
3127
|
+
}
|
|
3128
|
+
}
|
|
3129
|
+
if (resize)
|
|
3130
|
+
cbuf(bt + 131072);
|
|
3131
|
+
var lms = (1 << lbt) - 1, dms = (1 << dbt) - 1;
|
|
3132
|
+
var lpos = pos;
|
|
3133
|
+
for (; ; lpos = pos) {
|
|
3134
|
+
var c = lm[bits16(dat, pos) & lms], sym = c >> 4;
|
|
3135
|
+
pos += c & 15;
|
|
3136
|
+
if (pos > tbts) {
|
|
3137
|
+
if (noSt)
|
|
3138
|
+
err(0);
|
|
3139
|
+
break;
|
|
3140
|
+
}
|
|
3141
|
+
if (!c)
|
|
3142
|
+
err(2);
|
|
3143
|
+
if (sym < 256)
|
|
3144
|
+
buf[bt++] = sym;
|
|
3145
|
+
else if (sym == 256) {
|
|
3146
|
+
lpos = pos, lm = null;
|
|
3147
|
+
break;
|
|
3148
|
+
} else {
|
|
3149
|
+
var add = sym - 254;
|
|
3150
|
+
if (sym > 264) {
|
|
3151
|
+
var i2 = sym - 257, b2 = fleb[i2];
|
|
3152
|
+
add = bits(dat, pos, (1 << b2) - 1) + fl[i2];
|
|
3153
|
+
pos += b2;
|
|
3154
|
+
}
|
|
3155
|
+
var d = dm[bits16(dat, pos) & dms], dsym = d >> 4;
|
|
3156
|
+
if (!d)
|
|
3157
|
+
err(3);
|
|
3158
|
+
pos += d & 15;
|
|
3159
|
+
var dt = fd[dsym];
|
|
3160
|
+
if (dsym > 3) {
|
|
3161
|
+
var b2 = fdeb[dsym];
|
|
3162
|
+
dt += bits16(dat, pos) & (1 << b2) - 1, pos += b2;
|
|
3163
|
+
}
|
|
3164
|
+
if (pos > tbts) {
|
|
3165
|
+
if (noSt)
|
|
3166
|
+
err(0);
|
|
3167
|
+
break;
|
|
3168
|
+
}
|
|
3169
|
+
if (resize)
|
|
3170
|
+
cbuf(bt + 131072);
|
|
3171
|
+
var end = bt + add;
|
|
3172
|
+
if (bt < dt) {
|
|
3173
|
+
var shift = dl - dt, dend = Math.min(dt, end);
|
|
3174
|
+
if (shift + bt < 0)
|
|
3175
|
+
err(3);
|
|
3176
|
+
for (; bt < dend; ++bt)
|
|
3177
|
+
buf[bt] = dict[shift + bt];
|
|
3178
|
+
}
|
|
3179
|
+
for (; bt < end; ++bt)
|
|
3180
|
+
buf[bt] = buf[bt - dt];
|
|
3181
|
+
}
|
|
3182
|
+
}
|
|
3183
|
+
st.l = lm, st.p = lpos, st.b = bt, st.f = final;
|
|
3184
|
+
if (lm)
|
|
3185
|
+
final = 1, st.m = lbt, st.d = dm, st.n = dbt;
|
|
3186
|
+
} while (!final);
|
|
3187
|
+
return bt != buf.length && noBuf ? slc(buf, 0, bt) : buf.subarray(0, bt);
|
|
3188
|
+
};
|
|
3189
|
+
var et = /* @__PURE__ */ new u8(0);
|
|
3190
|
+
var gzs = function(d) {
|
|
3191
|
+
if (d[0] != 31 || d[1] != 139 || d[2] != 8)
|
|
3192
|
+
err(6, "invalid gzip data");
|
|
3193
|
+
var flg = d[3];
|
|
3194
|
+
var st = 10;
|
|
3195
|
+
if (flg & 4)
|
|
3196
|
+
st += (d[10] | d[11] << 8) + 2;
|
|
3197
|
+
for (var zs = (flg >> 3 & 1) + (flg >> 4 & 1); zs > 0; zs -= !d[st++])
|
|
3198
|
+
;
|
|
3199
|
+
return st + (flg & 2);
|
|
3200
|
+
};
|
|
3201
|
+
var gzl = function(d) {
|
|
3202
|
+
var l2 = d.length;
|
|
3203
|
+
return (d[l2 - 4] | d[l2 - 3] << 8 | d[l2 - 2] << 16 | d[l2 - 1] << 24) >>> 0;
|
|
3204
|
+
};
|
|
3205
|
+
var zls = function(d, dict) {
|
|
3206
|
+
if ((d[0] & 15) != 8 || d[0] >> 4 > 7 || (d[0] << 8 | d[1]) % 31)
|
|
3207
|
+
err(6, "invalid zlib data");
|
|
3208
|
+
if ((d[1] >> 5 & 1) == +!dict)
|
|
3209
|
+
err(6, "invalid zlib data: " + (d[1] & 32 ? "need" : "unexpected") + " dictionary");
|
|
3210
|
+
return (d[1] >> 3 & 4) + 2;
|
|
3211
|
+
};
|
|
3212
|
+
function inflateSync(data, opts) {
|
|
3213
|
+
return inflt(data, { i: 2 }, opts && opts.out, opts && opts.dictionary);
|
|
3214
|
+
}
|
|
3215
|
+
function gunzipSync(data, opts) {
|
|
3216
|
+
var st = gzs(data);
|
|
3217
|
+
if (st + 8 > data.length)
|
|
3218
|
+
err(6, "invalid gzip data");
|
|
3219
|
+
return inflt(data.subarray(st, -8), { i: 2 }, opts && opts.out || new u8(gzl(data)), opts && opts.dictionary);
|
|
3220
|
+
}
|
|
3221
|
+
function unzlibSync(data, opts) {
|
|
3222
|
+
return inflt(data.subarray(zls(data, opts && opts.dictionary), -4), { i: 2 }, opts && opts.out, opts && opts.dictionary);
|
|
3223
|
+
}
|
|
3224
|
+
function decompressSync(data, opts) {
|
|
3225
|
+
return data[0] == 31 && data[1] == 139 && data[2] == 8 ? gunzipSync(data, opts) : (data[0] & 15) != 8 || data[0] >> 4 > 7 || (data[0] << 8 | data[1]) % 31 ? inflateSync(data, opts) : unzlibSync(data, opts);
|
|
3226
|
+
}
|
|
3227
|
+
var td = typeof TextDecoder != "undefined" && /* @__PURE__ */ new TextDecoder();
|
|
3228
|
+
var tds = 0;
|
|
3229
|
+
try {
|
|
3230
|
+
td.decode(et, { stream: true });
|
|
3231
|
+
tds = 1;
|
|
3232
|
+
} catch (e) {
|
|
3233
|
+
}
|
|
3234
|
+
|
|
3235
|
+
// ../../node_modules/.pnpm/pmtiles@4.4.0/node_modules/pmtiles/dist/esm/index.js
|
|
3236
|
+
var j = Object.defineProperty;
|
|
3237
|
+
var B = Math.pow;
|
|
3238
|
+
var l = (o, t) => j(o, "name", { value: t, configurable: true });
|
|
3239
|
+
var m = (o, t, e) => new Promise((r, n) => {
|
|
3240
|
+
var s = (c) => {
|
|
3241
|
+
try {
|
|
3242
|
+
a(e.next(c));
|
|
3243
|
+
} catch (u) {
|
|
3244
|
+
n(u);
|
|
3245
|
+
}
|
|
3246
|
+
}, i2 = (c) => {
|
|
3247
|
+
try {
|
|
3248
|
+
a(e.throw(c));
|
|
3249
|
+
} catch (u) {
|
|
3250
|
+
n(u);
|
|
3251
|
+
}
|
|
3252
|
+
}, a = (c) => c.done ? r(c.value) : Promise.resolve(c.value).then(s, i2);
|
|
3253
|
+
a((e = e.apply(o, t)).next());
|
|
3254
|
+
});
|
|
3255
|
+
var re = l((o, t) => {
|
|
3256
|
+
let e = false, r = "", n = L.GridLayer.extend({ createTile: l((s, i2) => {
|
|
3257
|
+
let a = document.createElement("img"), c = new AbortController(), u = c.signal;
|
|
3258
|
+
return a.cancel = () => {
|
|
3259
|
+
c.abort();
|
|
3260
|
+
}, e || (o.getHeader().then((d) => {
|
|
3261
|
+
d.tileType === 1 ? console.error("Error: archive contains MVT vector tiles, but leafletRasterLayer is for displaying raster tiles. See https://github.com/protomaps/PMTiles/tree/main/js for details.") : d.tileType === 2 ? r = "image/png" : d.tileType === 3 ? r = "image/jpeg" : d.tileType === 4 ? r = "image/webp" : d.tileType === 5 && (r = "image/avif");
|
|
3262
|
+
}), e = true), o.getZxy(s.z, s.x, s.y, u).then((d) => {
|
|
3263
|
+
if (d) {
|
|
3264
|
+
let h = new Blob([d.data], { type: r }), p = window.URL.createObjectURL(h);
|
|
3265
|
+
a.src = p;
|
|
3266
|
+
} else a.style.display = "none";
|
|
3267
|
+
a.cancel = void 0, i2(void 0, a);
|
|
3268
|
+
}).catch((d) => {
|
|
3269
|
+
if (d.name !== "AbortError") throw d;
|
|
3270
|
+
}), a;
|
|
3271
|
+
}, "createTile"), _removeTile: l(function(s) {
|
|
3272
|
+
let i2 = this._tiles[s];
|
|
3273
|
+
i2 && (i2.el.cancel && i2.el.cancel(), i2.el.width = 0, i2.el.height = 0, i2.el.deleted = true, L.DomUtil.remove(i2.el), delete this._tiles[s], this.fire("tileunload", { tile: i2.el, coords: this._keyToTileCoords(s) }));
|
|
3274
|
+
}, "_removeTile") });
|
|
3275
|
+
return new n(t);
|
|
3276
|
+
}, "leafletRasterLayer");
|
|
3277
|
+
var W = l((o) => (t, e) => {
|
|
3278
|
+
if (e instanceof AbortController) return o(t, e);
|
|
3279
|
+
let r = new AbortController();
|
|
3280
|
+
return o(t, r).then((n) => e(void 0, n.data, n.cacheControl || "", n.expires || ""), (n) => e(n)).catch((n) => e(n)), { cancel: l(() => r.abort(), "cancel") };
|
|
3281
|
+
}, "v3compat");
|
|
3282
|
+
var E = class E2 {
|
|
3283
|
+
constructor(t) {
|
|
3284
|
+
this.tilev4 = l((t2, e) => m(this, null, function* () {
|
|
3285
|
+
if (t2.type === "json") {
|
|
3286
|
+
let p = t2.url.substr(10), y = this.tiles.get(p);
|
|
3287
|
+
if (y || (y = new w(p), this.tiles.set(p, y)), this.metadata) {
|
|
3288
|
+
let z = yield y.getTileJson(t2.url);
|
|
3289
|
+
return e.signal.throwIfAborted(), { data: z };
|
|
3290
|
+
}
|
|
3291
|
+
let f = yield y.getHeader();
|
|
3292
|
+
return e.signal.throwIfAborted(), (f.minLon >= f.maxLon || f.minLat >= f.maxLat) && console.error(`Bounds of PMTiles archive ${f.minLon},${f.minLat},${f.maxLon},${f.maxLat} are not valid.`), { data: { tiles: [`${t2.url}/{z}/{x}/{y}`], minzoom: f.minZoom, maxzoom: f.maxZoom, bounds: [f.minLon, f.minLat, f.maxLon, f.maxLat] } };
|
|
3293
|
+
}
|
|
3294
|
+
let r = new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/), n = t2.url.match(r);
|
|
3295
|
+
if (!n) throw new Error("Invalid PMTiles protocol URL");
|
|
3296
|
+
let s = n[1], i2 = this.tiles.get(s);
|
|
3297
|
+
i2 || (i2 = new w(s), this.tiles.set(s, i2));
|
|
3298
|
+
let a = n[2], c = n[3], u = n[4], d = yield i2.getHeader(), h = yield i2 == null ? void 0 : i2.getZxy(+a, +c, +u, e.signal);
|
|
3299
|
+
if (e.signal.throwIfAborted(), h) return { data: new Uint8Array(h.data), cacheControl: h.cacheControl, expires: h.expires };
|
|
3300
|
+
if (d.tileType === 1) {
|
|
3301
|
+
if (this.errorOnMissingTile) throw new Error("Tile not found.");
|
|
3302
|
+
return { data: new Uint8Array() };
|
|
3303
|
+
}
|
|
3304
|
+
return { data: null };
|
|
3305
|
+
}), "tilev4");
|
|
3306
|
+
this.tile = W(this.tilev4);
|
|
3307
|
+
this.tiles = /* @__PURE__ */ new Map(), this.metadata = (t == null ? void 0 : t.metadata) || false, this.errorOnMissingTile = (t == null ? void 0 : t.errorOnMissingTile) || false;
|
|
3308
|
+
}
|
|
3309
|
+
add(t) {
|
|
3310
|
+
this.tiles.set(t.source.getKey(), t);
|
|
3311
|
+
}
|
|
3312
|
+
get(t) {
|
|
3313
|
+
return this.tiles.get(t);
|
|
3314
|
+
}
|
|
3315
|
+
};
|
|
3316
|
+
l(E, "Protocol");
|
|
3317
|
+
function b(o, t) {
|
|
3318
|
+
return (t >>> 0) * 4294967296 + (o >>> 0);
|
|
3319
|
+
}
|
|
3320
|
+
l(b, "toNum");
|
|
3321
|
+
function N(o, t) {
|
|
3322
|
+
let e = t.buf, r = e[t.pos++], n = (r & 112) >> 4;
|
|
3323
|
+
if (r < 128 || (r = e[t.pos++], n |= (r & 127) << 3, r < 128) || (r = e[t.pos++], n |= (r & 127) << 10, r < 128) || (r = e[t.pos++], n |= (r & 127) << 17, r < 128) || (r = e[t.pos++], n |= (r & 127) << 24, r < 128) || (r = e[t.pos++], n |= (r & 1) << 31, r < 128)) return b(o, n);
|
|
3324
|
+
throw new Error("Expected varint not more than 10 bytes");
|
|
3325
|
+
}
|
|
3326
|
+
l(N, "readVarintRemainder");
|
|
3327
|
+
function x2(o) {
|
|
3328
|
+
let t = o.buf, e = t[o.pos++], r = e & 127;
|
|
3329
|
+
return e < 128 || (e = t[o.pos++], r |= (e & 127) << 7, e < 128) || (e = t[o.pos++], r |= (e & 127) << 14, e < 128) || (e = t[o.pos++], r |= (e & 127) << 21, e < 128) ? r : (e = t[o.pos], r |= (e & 15) << 28, N(r, o));
|
|
3330
|
+
}
|
|
3331
|
+
l(x2, "readVarint");
|
|
3332
|
+
function k(o, t, e, r, n) {
|
|
3333
|
+
return n === 0 ? r !== 0 ? [o - 1 - e, o - 1 - t] : [e, t] : [t, e];
|
|
3334
|
+
}
|
|
3335
|
+
l(k, "rotate");
|
|
3336
|
+
function q(o, t, e) {
|
|
3337
|
+
if (o > 26) throw new Error("Tile zoom level exceeds max safe number limit (26)");
|
|
3338
|
+
if (t >= 1 << o || e >= 1 << o) throw new Error("tile x/y outside zoom level bounds");
|
|
3339
|
+
let r = ((1 << o) * (1 << o) - 1) / 3, n = o - 1, [s, i2] = [t, e];
|
|
3340
|
+
for (let a = 1 << n; a > 0; a >>= 1) {
|
|
3341
|
+
let c = s & a, u = i2 & a;
|
|
3342
|
+
r += (3 * c ^ u) * (1 << n), [s, i2] = k(a, s, i2, c, u), n--;
|
|
3343
|
+
}
|
|
3344
|
+
return r;
|
|
3345
|
+
}
|
|
3346
|
+
l(q, "zxyToTileId");
|
|
3347
|
+
function G(o) {
|
|
3348
|
+
let t = 3 * o + 1;
|
|
3349
|
+
return t < 4294967296 ? 31 - Math.clz32(t) : 63 - Math.clz32(t / 4294967296);
|
|
3350
|
+
}
|
|
3351
|
+
l(G, "tileIdToZ");
|
|
3352
|
+
function ie(o) {
|
|
3353
|
+
let t = G(o) >> 1;
|
|
3354
|
+
if (t > 26) throw new Error("Tile zoom level exceeds max safe number limit (26)");
|
|
3355
|
+
let e = ((1 << t) * (1 << t) - 1) / 3, r = o - e, n = 0, s = 0, i2 = 1 << t;
|
|
3356
|
+
for (let a = 1; a < i2; a <<= 1) {
|
|
3357
|
+
let c = a & r / 2, u = a & (r ^ c);
|
|
3358
|
+
[n, s] = k(a, n, s, c, u), r = r / 2, n += c, s += u;
|
|
3359
|
+
}
|
|
3360
|
+
return [t, n, s];
|
|
3361
|
+
}
|
|
3362
|
+
l(ie, "tileIdToZxy");
|
|
3363
|
+
var J = ((s) => (s[s.Unknown = 0] = "Unknown", s[s.None = 1] = "None", s[s.Gzip = 2] = "Gzip", s[s.Brotli = 3] = "Brotli", s[s.Zstd = 4] = "Zstd", s))(J || {});
|
|
3364
|
+
function D(o, t) {
|
|
3365
|
+
return m(this, null, function* () {
|
|
3366
|
+
if (t === 1 || t === 0) return o;
|
|
3367
|
+
if (t === 2) {
|
|
3368
|
+
if (typeof globalThis.DecompressionStream == "undefined") return decompressSync(new Uint8Array(o));
|
|
3369
|
+
let e = new Response(o).body;
|
|
3370
|
+
if (!e) throw new Error("Failed to read response stream");
|
|
3371
|
+
let r = e.pipeThrough(new globalThis.DecompressionStream("gzip"));
|
|
3372
|
+
return new Response(r).arrayBuffer();
|
|
3373
|
+
}
|
|
3374
|
+
throw new Error("Compression method not supported");
|
|
3375
|
+
});
|
|
3376
|
+
}
|
|
3377
|
+
l(D, "defaultDecompress");
|
|
3378
|
+
var O = ((a) => (a[a.Unknown = 0] = "Unknown", a[a.Mvt = 1] = "Mvt", a[a.Png = 2] = "Png", a[a.Jpeg = 3] = "Jpeg", a[a.Webp = 4] = "Webp", a[a.Avif = 5] = "Avif", a[a.Mlt = 6] = "Mlt", a))(O || {});
|
|
3379
|
+
function _(o) {
|
|
3380
|
+
return o === 1 ? ".mvt" : o === 2 ? ".png" : o === 3 ? ".jpg" : o === 4 ? ".webp" : o === 5 ? ".avif" : o === 6 ? ".mlt" : "";
|
|
3381
|
+
}
|
|
3382
|
+
l(_, "tileTypeExt");
|
|
3383
|
+
var Y = 127;
|
|
3384
|
+
function Q(o, t) {
|
|
3385
|
+
let e = 0, r = o.length - 1;
|
|
3386
|
+
for (; e <= r; ) {
|
|
3387
|
+
let n = r + e >> 1, s = t - o[n].tileId;
|
|
3388
|
+
if (s > 0) e = n + 1;
|
|
3389
|
+
else if (s < 0) r = n - 1;
|
|
3390
|
+
else return o[n];
|
|
3391
|
+
}
|
|
3392
|
+
return r >= 0 && (o[r].runLength === 0 || t - o[r].tileId < o[r].runLength) ? o[r] : null;
|
|
3393
|
+
}
|
|
3394
|
+
l(Q, "findTile");
|
|
3395
|
+
var A = class A2 {
|
|
3396
|
+
constructor(t) {
|
|
3397
|
+
this.file = t;
|
|
10
3398
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return void 0;
|
|
3399
|
+
getKey() {
|
|
3400
|
+
return this.file.name;
|
|
14
3401
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
3402
|
+
getBytes(t, e) {
|
|
3403
|
+
return m(this, null, function* () {
|
|
3404
|
+
return { data: yield this.file.slice(t, t + e).arrayBuffer() };
|
|
3405
|
+
});
|
|
19
3406
|
}
|
|
20
|
-
|
|
21
|
-
|
|
3407
|
+
};
|
|
3408
|
+
l(A, "FileSource");
|
|
3409
|
+
var C = class C2 {
|
|
3410
|
+
constructor(t, e = new Headers()) {
|
|
3411
|
+
var i2, a;
|
|
3412
|
+
this.url = t, this.customHeaders = e, this.mustReload = false;
|
|
3413
|
+
let r = "";
|
|
3414
|
+
"navigator" in globalThis && (r = (a = (i2 = globalThis.navigator) == null ? void 0 : i2.userAgent) != null ? a : "");
|
|
3415
|
+
let n = r.indexOf("Windows") > -1, s = /Chrome|Chromium|Edg|OPR|Brave/.test(r);
|
|
3416
|
+
this.chromeWindowsNoCache = false, n && s && (this.chromeWindowsNoCache = true);
|
|
22
3417
|
}
|
|
23
|
-
|
|
3418
|
+
getKey() {
|
|
3419
|
+
return this.url;
|
|
3420
|
+
}
|
|
3421
|
+
setHeaders(t) {
|
|
3422
|
+
this.customHeaders = t;
|
|
3423
|
+
}
|
|
3424
|
+
getBytes(t, e, r, n) {
|
|
3425
|
+
return m(this, null, function* () {
|
|
3426
|
+
let s, i2;
|
|
3427
|
+
r ? i2 = r : (s = new AbortController(), i2 = s.signal);
|
|
3428
|
+
let a = new Headers(this.customHeaders);
|
|
3429
|
+
a.set("range", `bytes=${t}-${t + e - 1}`);
|
|
3430
|
+
let c;
|
|
3431
|
+
this.mustReload ? c = "reload" : this.chromeWindowsNoCache && (c = "no-store");
|
|
3432
|
+
let u = yield fetch(this.url, { signal: i2, cache: c, headers: a });
|
|
3433
|
+
if (t === 0 && u.status === 416) {
|
|
3434
|
+
let y = u.headers.get("Content-Range");
|
|
3435
|
+
if (!y || !y.startsWith("bytes */")) throw new Error("Missing content-length on 416 response");
|
|
3436
|
+
let f = +y.substr(8);
|
|
3437
|
+
u = yield fetch(this.url, { signal: i2, cache: "reload", headers: { range: `bytes=0-${f - 1}` } });
|
|
3438
|
+
}
|
|
3439
|
+
let d = u.headers.get("Etag");
|
|
3440
|
+
if (d != null && d.startsWith("W/") && (d = null), u.status === 416 || n && d && d !== n) throw this.mustReload = true, new v(`Server returned non-matching ETag ${n} after one retry. Check browser extensions and servers for issues that may affect correct ETag headers.`);
|
|
3441
|
+
if (u.status >= 300) throw new Error(`Bad response code: ${u.status}`);
|
|
3442
|
+
let h = u.headers.get("Content-Length");
|
|
3443
|
+
if (u.status === 200 && (!h || +h > e)) throw s && s.abort(), new Error("Server returned no content-length header or content-length exceeding request. Check that your storage backend supports HTTP Byte Serving.");
|
|
3444
|
+
return { data: yield u.arrayBuffer(), etag: d || void 0, cacheControl: u.headers.get("Cache-Control") || void 0, expires: u.headers.get("Expires") || void 0 };
|
|
3445
|
+
});
|
|
3446
|
+
}
|
|
3447
|
+
};
|
|
3448
|
+
l(C, "FetchSource");
|
|
3449
|
+
var T = C;
|
|
3450
|
+
function g(o, t) {
|
|
3451
|
+
let e = o.getUint32(t + 4, true), r = o.getUint32(t + 0, true);
|
|
3452
|
+
return e * B(2, 32) + r;
|
|
24
3453
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
3454
|
+
l(g, "getUint64");
|
|
3455
|
+
function X(o, t) {
|
|
3456
|
+
let e = new DataView(o), r = e.getUint8(7);
|
|
3457
|
+
if (r > 3) throw new Error(`Archive is spec version ${r} but this library supports up to spec version 3`);
|
|
3458
|
+
return { specVersion: r, rootDirectoryOffset: g(e, 8), rootDirectoryLength: g(e, 16), jsonMetadataOffset: g(e, 24), jsonMetadataLength: g(e, 32), leafDirectoryOffset: g(e, 40), leafDirectoryLength: g(e, 48), tileDataOffset: g(e, 56), tileDataLength: g(e, 64), numAddressedTiles: g(e, 72), numTileEntries: g(e, 80), numTileContents: g(e, 88), clustered: e.getUint8(96) === 1, internalCompression: e.getUint8(97), tileCompression: e.getUint8(98), tileType: e.getUint8(99), minZoom: e.getUint8(100), maxZoom: e.getUint8(101), minLon: e.getInt32(102, true) / 1e7, minLat: e.getInt32(106, true) / 1e7, maxLon: e.getInt32(110, true) / 1e7, maxLat: e.getInt32(114, true) / 1e7, centerZoom: e.getUint8(118), centerLon: e.getInt32(119, true) / 1e7, centerLat: e.getInt32(123, true) / 1e7, etag: t };
|
|
3459
|
+
}
|
|
3460
|
+
l(X, "bytesToHeader");
|
|
3461
|
+
function Z(o) {
|
|
3462
|
+
let t = { buf: new Uint8Array(o), pos: 0 }, e = x2(t), r = [], n = 0;
|
|
3463
|
+
for (let s = 0; s < e; s++) {
|
|
3464
|
+
let i2 = x2(t);
|
|
3465
|
+
r.push({ tileId: n + i2, offset: 0, length: 0, runLength: 1 }), n += i2;
|
|
28
3466
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
3467
|
+
for (let s = 0; s < e; s++) r[s].runLength = x2(t);
|
|
3468
|
+
for (let s = 0; s < e; s++) r[s].length = x2(t);
|
|
3469
|
+
for (let s = 0; s < e; s++) {
|
|
3470
|
+
let i2 = x2(t);
|
|
3471
|
+
i2 === 0 && s > 0 ? r[s].offset = r[s - 1].offset + r[s - 1].length : r[s].offset = i2 - 1;
|
|
3472
|
+
}
|
|
3473
|
+
return r;
|
|
3474
|
+
}
|
|
3475
|
+
l(Z, "deserializeIndex");
|
|
3476
|
+
var U = class U2 extends Error {
|
|
3477
|
+
};
|
|
3478
|
+
l(U, "EtagMismatch");
|
|
3479
|
+
var v = U;
|
|
3480
|
+
function I(o, t) {
|
|
3481
|
+
return m(this, null, function* () {
|
|
3482
|
+
let e = yield o.getBytes(0, 16384);
|
|
3483
|
+
if (new DataView(e.data).getUint16(0, true) !== 19792) throw new Error("Wrong magic number for PMTiles archive");
|
|
3484
|
+
let n = e.data.slice(0, Y), s = X(n, e.etag), i2 = e.data.slice(s.rootDirectoryOffset, s.rootDirectoryOffset + s.rootDirectoryLength), a = `${o.getKey()}|${s.etag || ""}|${s.rootDirectoryOffset}|${s.rootDirectoryLength}`, c = Z(yield t(i2, s.internalCompression));
|
|
3485
|
+
return [s, [a, c.length, c]];
|
|
39
3486
|
});
|
|
40
|
-
|
|
41
|
-
|
|
3487
|
+
}
|
|
3488
|
+
l(I, "getHeaderAndRoot");
|
|
3489
|
+
function K(o, t, e, r, n) {
|
|
3490
|
+
return m(this, null, function* () {
|
|
3491
|
+
let s = yield o.getBytes(e, r, void 0, n.etag), i2 = yield t(s.data, n.internalCompression), a = Z(i2);
|
|
3492
|
+
if (a.length === 0) throw new Error("Empty directory is invalid");
|
|
3493
|
+
return a;
|
|
3494
|
+
});
|
|
3495
|
+
}
|
|
3496
|
+
l(K, "getDirectory");
|
|
3497
|
+
var R = class R2 {
|
|
3498
|
+
constructor(t = 100, e = true, r = D) {
|
|
3499
|
+
this.cache = /* @__PURE__ */ new Map(), this.maxCacheEntries = t, this.counter = 1, this.decompress = r;
|
|
3500
|
+
}
|
|
3501
|
+
getHeader(t) {
|
|
3502
|
+
return m(this, null, function* () {
|
|
3503
|
+
let e = t.getKey(), r = this.cache.get(e);
|
|
3504
|
+
if (r) return r.lastUsed = this.counter++, r.data;
|
|
3505
|
+
let n = yield I(t, this.decompress);
|
|
3506
|
+
return n[1] && this.cache.set(n[1][0], { lastUsed: this.counter++, data: n[1][2] }), this.cache.set(e, { lastUsed: this.counter++, data: n[0] }), this.prune(), n[0];
|
|
3507
|
+
});
|
|
3508
|
+
}
|
|
3509
|
+
getDirectory(t, e, r, n) {
|
|
3510
|
+
return m(this, null, function* () {
|
|
3511
|
+
let s = `${t.getKey()}|${n.etag || ""}|${e}|${r}`, i2 = this.cache.get(s);
|
|
3512
|
+
if (i2) return i2.lastUsed = this.counter++, i2.data;
|
|
3513
|
+
let a = yield K(t, this.decompress, e, r, n);
|
|
3514
|
+
return this.cache.set(s, { lastUsed: this.counter++, data: a }), this.prune(), a;
|
|
3515
|
+
});
|
|
3516
|
+
}
|
|
3517
|
+
prune() {
|
|
3518
|
+
if (this.cache.size > this.maxCacheEntries) {
|
|
3519
|
+
let t = 1 / 0, e;
|
|
3520
|
+
this.cache.forEach((r, n) => {
|
|
3521
|
+
r.lastUsed < t && (t = r.lastUsed, e = n);
|
|
3522
|
+
}), e && this.cache.delete(e);
|
|
3523
|
+
}
|
|
3524
|
+
}
|
|
3525
|
+
invalidate(t) {
|
|
3526
|
+
return m(this, null, function* () {
|
|
3527
|
+
this.cache.delete(t.getKey());
|
|
3528
|
+
});
|
|
3529
|
+
}
|
|
3530
|
+
};
|
|
3531
|
+
l(R, "ResolvedValueCache");
|
|
3532
|
+
var M = class M2 {
|
|
3533
|
+
constructor(t = 100, e = true, r = D) {
|
|
3534
|
+
this.cache = /* @__PURE__ */ new Map(), this.invalidations = /* @__PURE__ */ new Map(), this.maxCacheEntries = t, this.counter = 1, this.decompress = r;
|
|
3535
|
+
}
|
|
3536
|
+
getHeader(t) {
|
|
3537
|
+
return m(this, null, function* () {
|
|
3538
|
+
let e = t.getKey(), r = this.cache.get(e);
|
|
3539
|
+
if (r) return r.lastUsed = this.counter++, yield r.data;
|
|
3540
|
+
let n = new Promise((s, i2) => {
|
|
3541
|
+
I(t, this.decompress).then((a) => {
|
|
3542
|
+
a[1] && this.cache.set(a[1][0], { lastUsed: this.counter++, data: Promise.resolve(a[1][2]) }), s(a[0]), this.prune();
|
|
3543
|
+
}).catch((a) => {
|
|
3544
|
+
i2(a);
|
|
3545
|
+
});
|
|
3546
|
+
});
|
|
3547
|
+
return this.cache.set(e, { lastUsed: this.counter++, data: n }), n;
|
|
3548
|
+
});
|
|
3549
|
+
}
|
|
3550
|
+
getDirectory(t, e, r, n) {
|
|
3551
|
+
return m(this, null, function* () {
|
|
3552
|
+
let s = `${t.getKey()}|${n.etag || ""}|${e}|${r}`, i2 = this.cache.get(s);
|
|
3553
|
+
if (i2) return i2.lastUsed = this.counter++, yield i2.data;
|
|
3554
|
+
let a = new Promise((c, u) => {
|
|
3555
|
+
K(t, this.decompress, e, r, n).then((d) => {
|
|
3556
|
+
c(d), this.prune();
|
|
3557
|
+
}).catch((d) => {
|
|
3558
|
+
u(d);
|
|
3559
|
+
});
|
|
3560
|
+
});
|
|
3561
|
+
return this.cache.set(s, { lastUsed: this.counter++, data: a }), a;
|
|
3562
|
+
});
|
|
3563
|
+
}
|
|
3564
|
+
prune() {
|
|
3565
|
+
if (this.cache.size >= this.maxCacheEntries) {
|
|
3566
|
+
let t = 1 / 0, e;
|
|
3567
|
+
this.cache.forEach((r, n) => {
|
|
3568
|
+
r.lastUsed < t && (t = r.lastUsed, e = n);
|
|
3569
|
+
}), e && this.cache.delete(e);
|
|
3570
|
+
}
|
|
3571
|
+
}
|
|
3572
|
+
invalidate(t) {
|
|
3573
|
+
return m(this, null, function* () {
|
|
3574
|
+
let e = t.getKey();
|
|
3575
|
+
if (this.invalidations.get(e)) return yield this.invalidations.get(e);
|
|
3576
|
+
this.cache.delete(t.getKey());
|
|
3577
|
+
let r = new Promise((n, s) => {
|
|
3578
|
+
this.getHeader(t).then((i2) => {
|
|
3579
|
+
n(), this.invalidations.delete(e);
|
|
3580
|
+
}).catch((i2) => {
|
|
3581
|
+
s(i2);
|
|
3582
|
+
});
|
|
3583
|
+
});
|
|
3584
|
+
this.invalidations.set(e, r);
|
|
3585
|
+
});
|
|
3586
|
+
}
|
|
3587
|
+
};
|
|
3588
|
+
l(M, "SharedPromiseCache");
|
|
3589
|
+
var P = M;
|
|
3590
|
+
var H = class H2 {
|
|
3591
|
+
constructor(t, e, r) {
|
|
3592
|
+
typeof t == "string" ? this.source = new T(t) : this.source = t, r ? this.decompress = r : this.decompress = D, e ? this.cache = e : this.cache = new P();
|
|
3593
|
+
}
|
|
3594
|
+
getHeader() {
|
|
3595
|
+
return m(this, null, function* () {
|
|
3596
|
+
return yield this.cache.getHeader(this.source);
|
|
3597
|
+
});
|
|
3598
|
+
}
|
|
3599
|
+
getZxyAttempt(t, e, r, n) {
|
|
3600
|
+
return m(this, null, function* () {
|
|
3601
|
+
let s = q(t, e, r), i2 = yield this.cache.getHeader(this.source);
|
|
3602
|
+
if (t < i2.minZoom || t > i2.maxZoom) return;
|
|
3603
|
+
let a = i2.rootDirectoryOffset, c = i2.rootDirectoryLength;
|
|
3604
|
+
for (let u = 0; u <= 3; u++) {
|
|
3605
|
+
let d = yield this.cache.getDirectory(this.source, a, c, i2), h = Q(d, s);
|
|
3606
|
+
if (h) {
|
|
3607
|
+
if (h.runLength > 0) {
|
|
3608
|
+
let p = yield this.source.getBytes(i2.tileDataOffset + h.offset, h.length, n, i2.etag);
|
|
3609
|
+
return { data: yield this.decompress(p.data, i2.tileCompression), cacheControl: p.cacheControl, expires: p.expires };
|
|
3610
|
+
}
|
|
3611
|
+
a = i2.leafDirectoryOffset + h.offset, c = h.length;
|
|
3612
|
+
} else return;
|
|
3613
|
+
}
|
|
3614
|
+
throw new Error("Maximum directory depth exceeded");
|
|
3615
|
+
});
|
|
3616
|
+
}
|
|
3617
|
+
getZxy(t, e, r, n) {
|
|
3618
|
+
return m(this, null, function* () {
|
|
3619
|
+
try {
|
|
3620
|
+
return yield this.getZxyAttempt(t, e, r, n);
|
|
3621
|
+
} catch (s) {
|
|
3622
|
+
if (s instanceof v) return this.cache.invalidate(this.source), yield this.getZxyAttempt(t, e, r, n);
|
|
3623
|
+
throw s;
|
|
3624
|
+
}
|
|
3625
|
+
});
|
|
3626
|
+
}
|
|
3627
|
+
getMetadataAttempt() {
|
|
3628
|
+
return m(this, null, function* () {
|
|
3629
|
+
let t = yield this.cache.getHeader(this.source), e = yield this.source.getBytes(t.jsonMetadataOffset, t.jsonMetadataLength, void 0, t.etag), r = yield this.decompress(e.data, t.internalCompression), n = new TextDecoder("utf-8");
|
|
3630
|
+
return JSON.parse(n.decode(r));
|
|
3631
|
+
});
|
|
3632
|
+
}
|
|
3633
|
+
getMetadata() {
|
|
3634
|
+
return m(this, null, function* () {
|
|
3635
|
+
try {
|
|
3636
|
+
return yield this.getMetadataAttempt();
|
|
3637
|
+
} catch (t) {
|
|
3638
|
+
if (t instanceof v) return this.cache.invalidate(this.source), yield this.getMetadataAttempt();
|
|
3639
|
+
throw t;
|
|
3640
|
+
}
|
|
3641
|
+
});
|
|
3642
|
+
}
|
|
3643
|
+
getTileJson(t) {
|
|
3644
|
+
return m(this, null, function* () {
|
|
3645
|
+
let e = yield this.getHeader(), r = yield this.getMetadata(), n = _(e.tileType);
|
|
3646
|
+
return { tilejson: "3.0.0", scheme: "xyz", tiles: [`${t}/{z}/{x}/{y}${n}`], vector_layers: r.vector_layers, attribution: r.attribution, description: r.description, name: r.name, version: r.version, bounds: [e.minLon, e.minLat, e.maxLon, e.maxLat], center: [e.centerLon, e.centerLat, e.centerZoom], minzoom: e.minZoom, maxzoom: e.maxZoom };
|
|
3647
|
+
});
|
|
3648
|
+
}
|
|
3649
|
+
};
|
|
3650
|
+
l(H, "PMTiles");
|
|
3651
|
+
var w = H;
|
|
3652
|
+
|
|
3653
|
+
// src/pmtiles-browser.ts
|
|
3654
|
+
var registeredPmtilesProtocols = /* @__PURE__ */ new WeakSet();
|
|
3655
|
+
var maximumSectionBytes = 16 * 1024 * 1024;
|
|
3656
|
+
var maximumParserBytes = 32 * 1024 * 1024;
|
|
3657
|
+
var estimatedEntryBytes = 128;
|
|
3658
|
+
var tileflowPmtilesMaximumDirectoryDepth = 32;
|
|
3659
|
+
var tileflowPmtilesProtocol = "tileflow-pmtiles";
|
|
3660
|
+
var protocolPrefix = `${tileflowPmtilesProtocol}://`;
|
|
3661
|
+
function registerTileflowPmtilesProtocol(registry, options = {}) {
|
|
3662
|
+
if (registeredPmtilesProtocols.has(registry.addProtocol)) return;
|
|
3663
|
+
const maximumDirectoryDepth = normalizeMaximumDirectoryDepth(options.maximumDirectoryDepth);
|
|
3664
|
+
registry.addProtocol(
|
|
3665
|
+
tileflowPmtilesProtocol,
|
|
3666
|
+
new TileflowPmtilesProtocol(maximumDirectoryDepth).tile
|
|
3667
|
+
);
|
|
3668
|
+
registeredPmtilesProtocols.add(registry.addProtocol);
|
|
3669
|
+
}
|
|
3670
|
+
var TileflowPmtilesProtocol = class {
|
|
3671
|
+
constructor(maximumDirectoryDepth) {
|
|
3672
|
+
this.maximumDirectoryDepth = maximumDirectoryDepth;
|
|
3673
|
+
}
|
|
3674
|
+
maximumDirectoryDepth;
|
|
3675
|
+
#archives = /* @__PURE__ */ new Map();
|
|
3676
|
+
tilev4 = async (request, abortController) => {
|
|
3677
|
+
if (request.type === "json") {
|
|
3678
|
+
const archive2 = this.#archive(parseArchiveTarget(request.url));
|
|
3679
|
+
const header2 = await archive2.getHeader();
|
|
3680
|
+
const tileJson = await archive2.getTileJson(request.url);
|
|
3681
|
+
const data = header2.tileType === O.Mlt && isRecord2(tileJson) ? { ...tileJson, encoding: "mlt" } : tileJson;
|
|
3682
|
+
abortController.signal.throwIfAborted();
|
|
3683
|
+
return { data };
|
|
3684
|
+
}
|
|
3685
|
+
const parsed = parseTileTarget(request.url);
|
|
3686
|
+
const archive = this.#archive(parsed.archive);
|
|
3687
|
+
const header = await archive.getHeader();
|
|
3688
|
+
const response = await archive.getZxy(parsed.z, parsed.x, parsed.y, abortController.signal);
|
|
3689
|
+
abortController.signal.throwIfAborted();
|
|
3690
|
+
if (response) {
|
|
3691
|
+
return {
|
|
3692
|
+
data: new Uint8Array(response.data),
|
|
3693
|
+
...response.cacheControl ? { cacheControl: response.cacheControl } : {},
|
|
3694
|
+
...response.expires ? { expires: response.expires } : {}
|
|
3695
|
+
};
|
|
3696
|
+
}
|
|
3697
|
+
return {
|
|
3698
|
+
data: header.tileType === O.Mvt || header.tileType === O.Mlt ? new Uint8Array() : null
|
|
3699
|
+
};
|
|
3700
|
+
};
|
|
3701
|
+
tile = protocolCompatibility(this.tilev4);
|
|
3702
|
+
#archive(target) {
|
|
3703
|
+
let archive = this.#archives.get(target);
|
|
3704
|
+
if (!archive) {
|
|
3705
|
+
archive = new TileflowPmtilesArchive(target, this.maximumDirectoryDepth);
|
|
3706
|
+
this.#archives.set(target, archive);
|
|
3707
|
+
}
|
|
3708
|
+
return archive;
|
|
3709
|
+
}
|
|
3710
|
+
};
|
|
3711
|
+
var TileflowPmtilesArchive = class extends w {
|
|
3712
|
+
constructor(source, maximumDirectoryDepth) {
|
|
3713
|
+
super(source, void 0, boundedDecompress);
|
|
3714
|
+
this.maximumDirectoryDepth = maximumDirectoryDepth;
|
|
3715
|
+
}
|
|
3716
|
+
maximumDirectoryDepth;
|
|
3717
|
+
async getZxyAttempt(z, x3, y, signal) {
|
|
3718
|
+
const tileId = q(z, x3, y);
|
|
3719
|
+
const header = await this.cache.getHeader(this.source);
|
|
3720
|
+
signal?.throwIfAborted();
|
|
3721
|
+
if (z < header.minZoom || z > header.maxZoom) return void 0;
|
|
3722
|
+
let offset = header.rootDirectoryOffset;
|
|
3723
|
+
let length = header.rootDirectoryLength;
|
|
3724
|
+
const ancestors = /* @__PURE__ */ new Set();
|
|
3725
|
+
let entriesRead = 0;
|
|
3726
|
+
for (let depth = 0; depth <= this.maximumDirectoryDepth; depth += 1) {
|
|
3727
|
+
const key = `${offset}:${length}`;
|
|
3728
|
+
if (ancestors.has(key)) throw new Error("Tileflow PMTiles directory cycle is invalid.");
|
|
3729
|
+
ancestors.add(key);
|
|
3730
|
+
const directory = await this.cache.getDirectory(this.source, offset, length, header);
|
|
3731
|
+
signal?.throwIfAborted();
|
|
3732
|
+
entriesRead += directory.length;
|
|
3733
|
+
if (entriesRead * estimatedEntryBytes > maximumParserBytes) {
|
|
3734
|
+
throw new Error("Tileflow PMTiles traversal exceeded its memory budget.");
|
|
3735
|
+
}
|
|
3736
|
+
const entry = Q(directory, tileId);
|
|
3737
|
+
if (!entry) return void 0;
|
|
3738
|
+
if (entry.runLength > 0) {
|
|
3739
|
+
assertRelativeRange(entry.offset, entry.length, header.tileDataLength ?? 0);
|
|
3740
|
+
const response = await this.source.getBytes(
|
|
3741
|
+
header.tileDataOffset + entry.offset,
|
|
3742
|
+
entry.length,
|
|
3743
|
+
signal,
|
|
3744
|
+
header.etag
|
|
3745
|
+
);
|
|
3746
|
+
return {
|
|
3747
|
+
data: await this.decompress(response.data, header.tileCompression),
|
|
3748
|
+
cacheControl: response.cacheControl,
|
|
3749
|
+
expires: response.expires
|
|
3750
|
+
};
|
|
3751
|
+
}
|
|
3752
|
+
assertRelativeRange(entry.offset, entry.length, header.leafDirectoryLength ?? 0);
|
|
3753
|
+
offset = header.leafDirectoryOffset + entry.offset;
|
|
3754
|
+
length = entry.length;
|
|
3755
|
+
}
|
|
3756
|
+
throw new Error("Tileflow PMTiles directory depth exceeds its operational limit.");
|
|
3757
|
+
}
|
|
3758
|
+
};
|
|
3759
|
+
async function boundedDecompress(buffer, compression) {
|
|
3760
|
+
if (buffer.byteLength > maximumSectionBytes) {
|
|
3761
|
+
throw new Error("Tileflow PMTiles section exceeds its operational limit.");
|
|
3762
|
+
}
|
|
3763
|
+
if (compression === J.None) return buffer;
|
|
3764
|
+
if (compression !== J.Gzip) {
|
|
3765
|
+
throw new Error("Tileflow PMTiles compression is unsupported.");
|
|
3766
|
+
}
|
|
3767
|
+
const stream = new Blob([buffer]).stream().pipeThrough(new DecompressionStream("gzip"));
|
|
3768
|
+
const reader = stream.getReader();
|
|
3769
|
+
const chunks = [];
|
|
3770
|
+
let byteLength = 0;
|
|
3771
|
+
try {
|
|
3772
|
+
while (true) {
|
|
3773
|
+
const result = await reader.read();
|
|
3774
|
+
if (result.done) break;
|
|
3775
|
+
byteLength += result.value.byteLength;
|
|
3776
|
+
if (byteLength > maximumSectionBytes) {
|
|
3777
|
+
await reader.cancel();
|
|
3778
|
+
throw new Error("Tileflow PMTiles decompression exceeds its operational limit.");
|
|
3779
|
+
}
|
|
3780
|
+
chunks.push(result.value);
|
|
3781
|
+
}
|
|
3782
|
+
} finally {
|
|
3783
|
+
reader.releaseLock();
|
|
3784
|
+
}
|
|
3785
|
+
const output = new Uint8Array(byteLength);
|
|
3786
|
+
let offset = 0;
|
|
3787
|
+
for (const chunk of chunks) {
|
|
3788
|
+
output.set(chunk, offset);
|
|
3789
|
+
offset += chunk.byteLength;
|
|
3790
|
+
}
|
|
3791
|
+
return output.buffer;
|
|
3792
|
+
}
|
|
3793
|
+
function parseArchiveTarget(url) {
|
|
3794
|
+
if (!url.startsWith(protocolPrefix)) throw new Error("Invalid Tileflow PMTiles protocol URL.");
|
|
3795
|
+
const target = url.slice(protocolPrefix.length);
|
|
3796
|
+
if (!target) throw new Error("Invalid Tileflow PMTiles archive URL.");
|
|
3797
|
+
return target;
|
|
3798
|
+
}
|
|
3799
|
+
function parseTileTarget(url) {
|
|
3800
|
+
const target = parseArchiveTarget(url);
|
|
3801
|
+
const match = /^(.*)\/(\d+)\/(\d+)\/(\d+)(?:\.[A-Za-z0-9]+)?$/u.exec(target);
|
|
3802
|
+
if (!match?.[1] || !match[2] || !match[3] || !match[4]) {
|
|
3803
|
+
throw new Error("Invalid Tileflow PMTiles tile URL.");
|
|
3804
|
+
}
|
|
3805
|
+
return { archive: match[1], x: Number(match[3]), y: Number(match[4]), z: Number(match[2]) };
|
|
3806
|
+
}
|
|
3807
|
+
function assertRelativeRange(offset, length, sectionLength) {
|
|
3808
|
+
if (!Number.isSafeInteger(offset) || !Number.isSafeInteger(length) || length < 1 || offset < 0 || offset > sectionLength - length || length > maximumSectionBytes) {
|
|
3809
|
+
throw new Error("Tileflow PMTiles directory range is invalid.");
|
|
3810
|
+
}
|
|
3811
|
+
}
|
|
3812
|
+
function normalizeMaximumDirectoryDepth(value) {
|
|
3813
|
+
const depth = value ?? tileflowPmtilesMaximumDirectoryDepth;
|
|
3814
|
+
if (!Number.isInteger(depth) || depth < 1 || depth > 64) {
|
|
3815
|
+
throw new Error("Tileflow PMTiles maximumDirectoryDepth must be an integer from 1 to 64.");
|
|
3816
|
+
}
|
|
3817
|
+
return depth;
|
|
3818
|
+
}
|
|
3819
|
+
function isRecord2(value) {
|
|
3820
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
3821
|
+
}
|
|
3822
|
+
function protocolCompatibility(handler) {
|
|
3823
|
+
return (request, controllerOrCallback) => {
|
|
3824
|
+
if (controllerOrCallback instanceof AbortController) {
|
|
3825
|
+
return handler(request, controllerOrCallback);
|
|
3826
|
+
}
|
|
3827
|
+
const controller = new AbortController();
|
|
3828
|
+
void handler(request, controller).then(
|
|
3829
|
+
(response) => controllerOrCallback(
|
|
3830
|
+
void 0,
|
|
3831
|
+
response.data,
|
|
3832
|
+
response.cacheControl ?? "",
|
|
3833
|
+
response.expires ?? ""
|
|
3834
|
+
),
|
|
3835
|
+
(error) => controllerOrCallback(error instanceof Error ? error : new Error("PMTiles request failed."))
|
|
3836
|
+
);
|
|
3837
|
+
return { cancel: () => controller.abort() };
|
|
3838
|
+
};
|
|
3839
|
+
}
|
|
3840
|
+
|
|
3841
|
+
// src/browser.ts
|
|
3842
|
+
var systemColorSchemeListeners = /* @__PURE__ */ new Set();
|
|
3843
|
+
var systemColorSchemeQuery;
|
|
3844
|
+
var systemColorScheme = "light";
|
|
3845
|
+
function getTileflowSystemColorScheme() {
|
|
3846
|
+
ensureSystemColorSchemeQuery();
|
|
3847
|
+
return systemColorScheme;
|
|
3848
|
+
}
|
|
3849
|
+
function subscribeTileflowSystemColorScheme(listener) {
|
|
3850
|
+
ensureSystemColorSchemeQuery();
|
|
3851
|
+
systemColorSchemeListeners.add(listener);
|
|
3852
|
+
return () => {
|
|
3853
|
+
systemColorSchemeListeners.delete(listener);
|
|
3854
|
+
if (systemColorSchemeListeners.size === 0 && systemColorSchemeQuery) {
|
|
3855
|
+
systemColorSchemeQuery.removeEventListener?.("change", handleSystemColorSchemeChange);
|
|
3856
|
+
systemColorSchemeQuery.removeListener?.(handleSystemColorSchemeChange);
|
|
3857
|
+
systemColorSchemeQuery = void 0;
|
|
3858
|
+
}
|
|
3859
|
+
};
|
|
3860
|
+
}
|
|
3861
|
+
function ensureSystemColorSchemeQuery() {
|
|
3862
|
+
if (systemColorSchemeQuery) return;
|
|
3863
|
+
const browser = globalThis;
|
|
3864
|
+
if (!browser.matchMedia) {
|
|
3865
|
+
systemColorScheme = "light";
|
|
42
3866
|
return;
|
|
43
3867
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
keepalive: true,
|
|
51
|
-
method: "POST"
|
|
52
|
-
}).catch(() => void 0);
|
|
3868
|
+
systemColorSchemeQuery = browser.matchMedia("(prefers-color-scheme: dark)");
|
|
3869
|
+
systemColorScheme = systemColorSchemeQuery.matches ? "dark" : "light";
|
|
3870
|
+
systemColorSchemeQuery.addEventListener?.("change", handleSystemColorSchemeChange);
|
|
3871
|
+
if (!systemColorSchemeQuery.addEventListener) {
|
|
3872
|
+
systemColorSchemeQuery.addListener?.(handleSystemColorSchemeChange);
|
|
3873
|
+
}
|
|
53
3874
|
}
|
|
54
|
-
function
|
|
55
|
-
if (
|
|
56
|
-
|
|
3875
|
+
function handleSystemColorSchemeChange() {
|
|
3876
|
+
if (!systemColorSchemeQuery) return;
|
|
3877
|
+
const next = systemColorSchemeQuery.matches ? "dark" : "light";
|
|
3878
|
+
if (next === systemColorScheme) return;
|
|
3879
|
+
systemColorScheme = next;
|
|
3880
|
+
for (const listener of systemColorSchemeListeners) listener(next);
|
|
3881
|
+
}
|
|
3882
|
+
function createTileflowThemeController(options) {
|
|
3883
|
+
assertConcreteRuntimeTheme(options.initial, "initial");
|
|
3884
|
+
const loadFonts = options.loadFonts ?? ((runtimeStyle) => loadTileflowStyleFonts(runtimeStyle.style, { fontFaces: runtimeStyle.fontFaces }));
|
|
3885
|
+
const timeoutMs = normalizeThemeTransitionTimeout(options.timeoutMs);
|
|
3886
|
+
let current = options.initial;
|
|
3887
|
+
let disposed = false;
|
|
3888
|
+
let requestId = 0;
|
|
3889
|
+
let applyQueue = Promise.resolve();
|
|
3890
|
+
return {
|
|
3891
|
+
dispose() {
|
|
3892
|
+
disposed = true;
|
|
3893
|
+
requestId += 1;
|
|
3894
|
+
},
|
|
3895
|
+
getCurrent() {
|
|
3896
|
+
return current;
|
|
3897
|
+
},
|
|
3898
|
+
setTheme(style) {
|
|
3899
|
+
const invalidTheme = validateConcreteRuntimeTheme(style, "target");
|
|
3900
|
+
if (invalidTheme) {
|
|
3901
|
+
return Promise.resolve({ error: invalidTheme, status: "failed", theme: style.theme });
|
|
3902
|
+
}
|
|
3903
|
+
if (disposed) {
|
|
3904
|
+
return Promise.resolve({
|
|
3905
|
+
error: new Error("Tileflow theme controller is disposed."),
|
|
3906
|
+
status: "failed",
|
|
3907
|
+
theme: style.theme
|
|
3908
|
+
});
|
|
3909
|
+
}
|
|
3910
|
+
const runId = ++requestId;
|
|
3911
|
+
options.onTransition?.({
|
|
3912
|
+
currentTheme: current.theme,
|
|
3913
|
+
phase: "preloading",
|
|
3914
|
+
targetTheme: style.theme
|
|
3915
|
+
});
|
|
3916
|
+
let preloaded;
|
|
3917
|
+
try {
|
|
3918
|
+
preloaded = loadFonts(style);
|
|
3919
|
+
} catch (error) {
|
|
3920
|
+
return Promise.resolve(handlePreloadFailure(runId, style, error));
|
|
3921
|
+
}
|
|
3922
|
+
return preloaded.then(
|
|
3923
|
+
() => enqueueTheme(runId, style),
|
|
3924
|
+
(error) => handlePreloadFailure(runId, style, error)
|
|
3925
|
+
);
|
|
3926
|
+
}
|
|
3927
|
+
};
|
|
3928
|
+
function enqueueTheme(runId, style) {
|
|
3929
|
+
const operation = applyQueue.then(
|
|
3930
|
+
() => applyTheme(runId, style),
|
|
3931
|
+
() => applyTheme(runId, style)
|
|
3932
|
+
);
|
|
3933
|
+
applyQueue = operation.then(
|
|
3934
|
+
() => void 0,
|
|
3935
|
+
() => void 0
|
|
3936
|
+
);
|
|
3937
|
+
return operation;
|
|
57
3938
|
}
|
|
58
|
-
|
|
3939
|
+
async function applyTheme(runId, style) {
|
|
3940
|
+
if (disposed || runId !== requestId) return { status: "superseded", theme: style.theme };
|
|
3941
|
+
const previous = current;
|
|
3942
|
+
options.onTransition?.({
|
|
3943
|
+
currentTheme: previous.theme,
|
|
3944
|
+
phase: "applying",
|
|
3945
|
+
targetTheme: style.theme
|
|
3946
|
+
});
|
|
59
3947
|
try {
|
|
60
|
-
|
|
61
|
-
return
|
|
62
|
-
|
|
63
|
-
|
|
3948
|
+
await applyMapStyle(options.map, style.style, timeoutMs);
|
|
3949
|
+
if (disposed) return { status: "superseded", theme: style.theme };
|
|
3950
|
+
if (runId !== requestId) {
|
|
3951
|
+
try {
|
|
3952
|
+
await applyMapStyle(options.map, current.style, timeoutMs);
|
|
3953
|
+
} catch {
|
|
3954
|
+
}
|
|
3955
|
+
return { status: "superseded", theme: style.theme };
|
|
3956
|
+
}
|
|
3957
|
+
current = style;
|
|
3958
|
+
options.onTransition?.({
|
|
3959
|
+
currentTheme: style.theme,
|
|
3960
|
+
phase: "ready",
|
|
3961
|
+
targetTheme: style.theme
|
|
3962
|
+
});
|
|
3963
|
+
return { status: "applied", theme: style.theme };
|
|
3964
|
+
} catch (error) {
|
|
3965
|
+
const normalized = normalizeThemeTransitionError(error, "Tileflow theme change failed.");
|
|
3966
|
+
try {
|
|
3967
|
+
await applyMapStyle(options.map, previous.style, timeoutMs);
|
|
3968
|
+
current = previous;
|
|
3969
|
+
} catch {
|
|
3970
|
+
}
|
|
3971
|
+
if (disposed || runId !== requestId) return { status: "superseded", theme: style.theme };
|
|
3972
|
+
options.onTransition?.({
|
|
3973
|
+
currentTheme: current.theme,
|
|
3974
|
+
error: normalized,
|
|
3975
|
+
phase: "error",
|
|
3976
|
+
targetTheme: style.theme
|
|
3977
|
+
});
|
|
3978
|
+
return { error: normalized, status: "failed", theme: style.theme };
|
|
64
3979
|
}
|
|
65
3980
|
}
|
|
66
|
-
|
|
3981
|
+
function handlePreloadFailure(runId, style, error) {
|
|
3982
|
+
if (disposed || runId !== requestId) return { status: "superseded", theme: style.theme };
|
|
3983
|
+
const normalized = normalizeThemeTransitionError(error, "Tileflow theme preload failed.");
|
|
3984
|
+
options.onTransition?.({
|
|
3985
|
+
currentTheme: current.theme,
|
|
3986
|
+
error: normalized,
|
|
3987
|
+
phase: "error",
|
|
3988
|
+
targetTheme: style.theme
|
|
3989
|
+
});
|
|
3990
|
+
return { error: normalized, status: "failed", theme: style.theme };
|
|
3991
|
+
}
|
|
67
3992
|
}
|
|
68
|
-
function
|
|
3993
|
+
function assertConcreteRuntimeTheme(style, role) {
|
|
3994
|
+
const error = validateConcreteRuntimeTheme(style, role);
|
|
3995
|
+
if (error) throw error;
|
|
3996
|
+
}
|
|
3997
|
+
function validateConcreteRuntimeTheme(style, role) {
|
|
3998
|
+
if (isTileflowThemeName(style.theme)) return void 0;
|
|
3999
|
+
return new TypeError(
|
|
4000
|
+
`Tileflow theme controller ${role} style requires a concrete portable theme name; received ${JSON.stringify(style.theme)}.`
|
|
4001
|
+
);
|
|
4002
|
+
}
|
|
4003
|
+
function applyMapStyle(map, style, timeoutMs) {
|
|
4004
|
+
return new Promise((resolve, reject) => {
|
|
4005
|
+
let settled = false;
|
|
4006
|
+
const cleanup = () => {
|
|
4007
|
+
clearTimeout(timeout);
|
|
4008
|
+
map.off("style.load", handleLoad);
|
|
4009
|
+
map.off("error", handleError);
|
|
4010
|
+
};
|
|
4011
|
+
const settle = (error) => {
|
|
4012
|
+
if (settled) return;
|
|
4013
|
+
settled = true;
|
|
4014
|
+
cleanup();
|
|
4015
|
+
if (error) reject(error);
|
|
4016
|
+
else resolve();
|
|
4017
|
+
};
|
|
4018
|
+
const handleLoad = () => settle();
|
|
4019
|
+
const handleError = (event) => settle(
|
|
4020
|
+
normalizeThemeTransitionError(
|
|
4021
|
+
isRecordWithError(event) ? event.error : event,
|
|
4022
|
+
"MapLibre rejected the Tileflow theme."
|
|
4023
|
+
)
|
|
4024
|
+
);
|
|
4025
|
+
const timeout = setTimeout(
|
|
4026
|
+
() => settle(new Error(`Tileflow theme change timed out after ${timeoutMs}ms.`)),
|
|
4027
|
+
timeoutMs
|
|
4028
|
+
);
|
|
4029
|
+
map.on("style.load", handleLoad);
|
|
4030
|
+
map.on("error", handleError);
|
|
4031
|
+
try {
|
|
4032
|
+
if (typeof map.setStyle !== "function") {
|
|
4033
|
+
throw new TypeError("Tileflow theme changes require MapLibre setStyle().");
|
|
4034
|
+
}
|
|
4035
|
+
const setStyle = map.setStyle;
|
|
4036
|
+
setStyle.call(map, style, { diff: true });
|
|
4037
|
+
} catch (error) {
|
|
4038
|
+
settle(normalizeThemeTransitionError(error, "MapLibre rejected the Tileflow theme."));
|
|
4039
|
+
}
|
|
4040
|
+
});
|
|
4041
|
+
}
|
|
4042
|
+
function normalizeThemeTransitionTimeout(value) {
|
|
4043
|
+
const resolved = value ?? 15e3;
|
|
4044
|
+
if (!Number.isSafeInteger(resolved) || resolved < 100 || resolved > 6e4) {
|
|
4045
|
+
throw new TypeError("Tileflow theme transition timeout must be an integer from 100 to 60000.");
|
|
4046
|
+
}
|
|
4047
|
+
return resolved;
|
|
4048
|
+
}
|
|
4049
|
+
function normalizeThemeTransitionError(error, fallback) {
|
|
4050
|
+
return error instanceof Error ? error : new Error(fallback);
|
|
4051
|
+
}
|
|
4052
|
+
function isRecordWithError(value) {
|
|
4053
|
+
return Boolean(value && typeof value === "object" && "error" in value);
|
|
4054
|
+
}
|
|
4055
|
+
var maximumTileflowFontBytes = 1024 * 1024;
|
|
4056
|
+
var maximumTileflowFontStyleBytes = 4 * 1024 * 1024;
|
|
4057
|
+
var loadedTileflowFontFaces = /* @__PURE__ */ new Map();
|
|
4058
|
+
async function loadTileflowStyleFonts(style, options = {}) {
|
|
4059
|
+
const fetcher = options.fetch ?? globalThis.fetch;
|
|
4060
|
+
const styleUrl = typeof style === "string" ? resolveTileflowBrowserResourceUrl(style) : void 0;
|
|
4061
|
+
const fontFaces = options.fontFaces === void 0 ? typeof style === "string" ? await fetchTileflowStyleFontFaces(styleUrl, fetcher) : getTileflowStyleFontFaces(style) : getTileflowStyleFontFaces({
|
|
4062
|
+
metadata: { "tileflow:fontFaces": [...options.fontFaces] }
|
|
4063
|
+
});
|
|
4064
|
+
if (fontFaces.length === 0) return;
|
|
4065
|
+
const browser = globalThis;
|
|
4066
|
+
const FontFaceConstructor = browser.FontFace;
|
|
4067
|
+
const fontSet = browser.document?.fonts;
|
|
4068
|
+
if (!FontFaceConstructor || !fontSet) {
|
|
4069
|
+
throw new Error("Tileflow web fonts require the browser FontFace API.");
|
|
4070
|
+
}
|
|
4071
|
+
await Promise.all(
|
|
4072
|
+
fontFaces.map(async (definition) => {
|
|
4073
|
+
const source = resolveTileflowBrowserResourceUrl(definition.source, styleUrl);
|
|
4074
|
+
const key = `${definition.family}\0${source}\0${definition.style ?? ""}\0${definition.weight ?? ""}`;
|
|
4075
|
+
let loaded = loadedTileflowFontFaces.get(key);
|
|
4076
|
+
if (!loaded) {
|
|
4077
|
+
loaded = loadTileflowFontFace(
|
|
4078
|
+
definition,
|
|
4079
|
+
source,
|
|
4080
|
+
fetcher,
|
|
4081
|
+
FontFaceConstructor,
|
|
4082
|
+
fontSet
|
|
4083
|
+
).catch((error) => {
|
|
4084
|
+
loadedTileflowFontFaces.delete(key);
|
|
4085
|
+
throw error;
|
|
4086
|
+
});
|
|
4087
|
+
loadedTileflowFontFaces.set(key, loaded);
|
|
4088
|
+
}
|
|
4089
|
+
await loaded;
|
|
4090
|
+
})
|
|
4091
|
+
);
|
|
4092
|
+
}
|
|
4093
|
+
async function fetchTileflowStyleFontFaces(styleUrl, fetcher) {
|
|
4094
|
+
const response = await fetcher(styleUrl, {
|
|
4095
|
+
cache: "default",
|
|
4096
|
+
credentials: "same-origin",
|
|
4097
|
+
redirect: "error"
|
|
4098
|
+
});
|
|
4099
|
+
if (!response.ok) throw new Error(`Tileflow style font metadata failed: ${response.status}`);
|
|
4100
|
+
const source = await readBoundedTileflowBrowserResource(
|
|
4101
|
+
response,
|
|
4102
|
+
maximumTileflowFontStyleBytes,
|
|
4103
|
+
"Tileflow style font metadata"
|
|
4104
|
+
);
|
|
4105
|
+
let input;
|
|
69
4106
|
try {
|
|
70
|
-
|
|
71
|
-
const parsedApiUrl = new URL(apiUrl);
|
|
72
|
-
return parsedUrl.origin === parsedApiUrl.origin && isTilePath(parsedUrl);
|
|
4107
|
+
input = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(source));
|
|
73
4108
|
} catch {
|
|
74
|
-
|
|
4109
|
+
throw new Error("Tileflow style font metadata is not valid JSON.");
|
|
4110
|
+
}
|
|
4111
|
+
if (!input || typeof input !== "object" || Array.isArray(input)) {
|
|
4112
|
+
throw new Error("Tileflow style font metadata is invalid.");
|
|
75
4113
|
}
|
|
4114
|
+
return getTileflowStyleFontFaces(input);
|
|
76
4115
|
}
|
|
77
|
-
function
|
|
78
|
-
|
|
4116
|
+
async function loadTileflowFontFace(definition, source, fetcher, FontFaceConstructor, fontSet) {
|
|
4117
|
+
const response = await fetcher(source, {
|
|
4118
|
+
cache: "force-cache",
|
|
4119
|
+
credentials: "same-origin",
|
|
4120
|
+
redirect: "error"
|
|
4121
|
+
});
|
|
4122
|
+
if (!response.ok) throw new Error(`Tileflow font failed: ${response.status}`);
|
|
4123
|
+
const bytes = await readBoundedTileflowBrowserResource(
|
|
4124
|
+
response,
|
|
4125
|
+
maximumTileflowFontBytes,
|
|
4126
|
+
"Tileflow font"
|
|
4127
|
+
);
|
|
4128
|
+
const fontFace = new FontFaceConstructor(definition.family, bytes.buffer, {
|
|
4129
|
+
...definition.style ? { style: definition.style } : {},
|
|
4130
|
+
...definition.weight ? { weight: definition.weight } : {}
|
|
4131
|
+
});
|
|
4132
|
+
await fontFace.load();
|
|
4133
|
+
fontSet.add(fontFace);
|
|
79
4134
|
}
|
|
80
|
-
function
|
|
81
|
-
|
|
82
|
-
|
|
4135
|
+
async function readBoundedTileflowBrowserResource(response, maximumBytes, label) {
|
|
4136
|
+
const contentLength = response.headers.get("content-length");
|
|
4137
|
+
if (contentLength && /^\d+$/u.test(contentLength) && Number(contentLength) > maximumBytes) {
|
|
4138
|
+
await response.body?.cancel().catch(() => void 0);
|
|
4139
|
+
throw new Error(`${label} exceeds the maximum response size.`);
|
|
83
4140
|
}
|
|
84
|
-
return
|
|
4141
|
+
if (!response.body) return new Uint8Array();
|
|
4142
|
+
const reader = response.body.getReader();
|
|
4143
|
+
const chunks = [];
|
|
4144
|
+
let byteLength = 0;
|
|
4145
|
+
try {
|
|
4146
|
+
while (true) {
|
|
4147
|
+
const { done, value } = await reader.read();
|
|
4148
|
+
if (done) break;
|
|
4149
|
+
if (!value) continue;
|
|
4150
|
+
byteLength += value.byteLength;
|
|
4151
|
+
if (byteLength > maximumBytes) {
|
|
4152
|
+
await reader.cancel().catch(() => void 0);
|
|
4153
|
+
throw new Error(`${label} exceeds the maximum response size.`);
|
|
4154
|
+
}
|
|
4155
|
+
chunks.push(value);
|
|
4156
|
+
}
|
|
4157
|
+
} finally {
|
|
4158
|
+
reader.releaseLock();
|
|
4159
|
+
}
|
|
4160
|
+
const bytes = new Uint8Array(byteLength);
|
|
4161
|
+
let offset = 0;
|
|
4162
|
+
for (const chunk of chunks) {
|
|
4163
|
+
bytes.set(chunk, offset);
|
|
4164
|
+
offset += chunk.byteLength;
|
|
4165
|
+
}
|
|
4166
|
+
return bytes;
|
|
4167
|
+
}
|
|
4168
|
+
function resolveTileflowBrowserResourceUrl(value, baseUrl) {
|
|
4169
|
+
const browser = globalThis;
|
|
4170
|
+
const base = baseUrl ?? browser.document?.baseURI ?? "http://localhost/";
|
|
4171
|
+
let url;
|
|
4172
|
+
try {
|
|
4173
|
+
url = new URL(value, base);
|
|
4174
|
+
} catch {
|
|
4175
|
+
throw new TypeError("Tileflow font resource URL is invalid.");
|
|
4176
|
+
}
|
|
4177
|
+
if (url.protocol !== "http:" && url.protocol !== "https:" || url.username || url.password || url.hash) {
|
|
4178
|
+
throw new TypeError("Tileflow font resource URL is invalid.");
|
|
4179
|
+
}
|
|
4180
|
+
return url.toString();
|
|
85
4181
|
}
|
|
86
|
-
|
|
87
|
-
// src/browser.ts
|
|
88
4182
|
function createTileflowSessionStarter(options) {
|
|
89
4183
|
const starts = /* @__PURE__ */ new Set();
|
|
90
4184
|
const send = options.startSession ?? startTileflowSession;
|
|
@@ -215,7 +4309,7 @@ function attachTileflowMapLifecycle(options) {
|
|
|
215
4309
|
};
|
|
216
4310
|
}
|
|
217
4311
|
function createTileflowTransformRequest(options) {
|
|
218
|
-
if (!options.always && !options.transformRequest) {
|
|
4312
|
+
if (!options.always && !options.transformRequest && !options.worldRequestBridge) {
|
|
219
4313
|
const analytics = options.getAnalytics();
|
|
220
4314
|
const requiresCommercialSession = Boolean(options.sessionController && analytics?.mapId);
|
|
221
4315
|
if (!requiresCommercialSession && (!analytics || analytics.enabled === false)) {
|
|
@@ -233,7 +4327,8 @@ function createTileflowTransformRequest(options) {
|
|
|
233
4327
|
resolvedRequest,
|
|
234
4328
|
asyncAnalyticsTiming === "resolution" ? options.getAnalytics() : analyticsAtRequest,
|
|
235
4329
|
options.sessionId,
|
|
236
|
-
options.sessionController
|
|
4330
|
+
options.sessionController,
|
|
4331
|
+
options.worldRequestBridge
|
|
237
4332
|
) ?? resolvedRequest
|
|
238
4333
|
);
|
|
239
4334
|
}
|
|
@@ -242,54 +4337,36 @@ function createTileflowTransformRequest(options) {
|
|
|
242
4337
|
request,
|
|
243
4338
|
asyncAnalyticsTiming === "resolution" ? options.getAnalytics() : analyticsAtRequest,
|
|
244
4339
|
options.sessionId,
|
|
245
|
-
options.sessionController
|
|
4340
|
+
options.sessionController,
|
|
4341
|
+
options.worldRequestBridge
|
|
246
4342
|
);
|
|
247
4343
|
};
|
|
248
4344
|
}
|
|
249
|
-
function
|
|
250
|
-
let markers = [];
|
|
251
|
-
const clear = () => {
|
|
252
|
-
const previousMarkers = markers;
|
|
253
|
-
markers = [];
|
|
254
|
-
removeMarkers(previousMarkers, options.remove);
|
|
255
|
-
};
|
|
256
|
-
return {
|
|
257
|
-
clear,
|
|
258
|
-
dispose: clear,
|
|
259
|
-
replace(map, definitions) {
|
|
260
|
-
clear();
|
|
261
|
-
const nextMarkers = [];
|
|
262
|
-
try {
|
|
263
|
-
for (const definition of definitions) {
|
|
264
|
-
const marker = options.create(definition);
|
|
265
|
-
nextMarkers.push(marker);
|
|
266
|
-
options.attach(marker, map, definition);
|
|
267
|
-
}
|
|
268
|
-
} catch (error) {
|
|
269
|
-
try {
|
|
270
|
-
removeMarkers(nextMarkers, options.remove);
|
|
271
|
-
} catch {
|
|
272
|
-
}
|
|
273
|
-
throw error;
|
|
274
|
-
}
|
|
275
|
-
markers = nextMarkers;
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
|
-
function applyTileflowRequest(url, request, analytics, sessionId, sessionController) {
|
|
4345
|
+
function applyTileflowRequest(url, request, analytics, sessionId, sessionController, worldRequestBridge) {
|
|
280
4346
|
const requestUrl = request?.url ?? url;
|
|
281
4347
|
if (sessionController) {
|
|
282
4348
|
return sessionController.resolveRequestUrl(requestUrl, analytics).then(
|
|
283
|
-
(nextUrl) =>
|
|
4349
|
+
(nextUrl) => applyWorldRequestBridge(
|
|
4350
|
+
composeTileflowRequest(url, request, nextUrl, true) ?? { url },
|
|
4351
|
+
worldRequestBridge
|
|
4352
|
+
) ?? { url }
|
|
284
4353
|
);
|
|
285
4354
|
}
|
|
286
|
-
return
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
4355
|
+
return applyWorldRequestBridge(
|
|
4356
|
+
composeTileflowRequest(
|
|
4357
|
+
url,
|
|
4358
|
+
request,
|
|
4359
|
+
resolveTileflowAnalyticsRequestUrl(requestUrl, analytics, sessionId),
|
|
4360
|
+
Boolean(worldRequestBridge)
|
|
4361
|
+
),
|
|
4362
|
+
worldRequestBridge
|
|
291
4363
|
);
|
|
292
4364
|
}
|
|
4365
|
+
function applyWorldRequestBridge(request, bridge) {
|
|
4366
|
+
if (!request || !bridge) return request;
|
|
4367
|
+
const url = bridge.rewriteUrl(request.url);
|
|
4368
|
+
return url === request.url ? request : { ...request, url };
|
|
4369
|
+
}
|
|
293
4370
|
function composeTileflowRequest(originalUrl, request, nextUrl, ensureRequest) {
|
|
294
4371
|
if (!nextUrl) {
|
|
295
4372
|
return request ?? (ensureRequest ? { url: originalUrl } : void 0);
|
|
@@ -314,24 +4391,38 @@ function disposeFunctions(disposers) {
|
|
|
314
4391
|
function isPromiseLike(value) {
|
|
315
4392
|
return Boolean(value && typeof value.then === "function");
|
|
316
4393
|
}
|
|
317
|
-
function removeMarkers(markers, remove) {
|
|
318
|
-
let firstError;
|
|
319
|
-
let failed = false;
|
|
320
|
-
for (const marker of markers) {
|
|
321
|
-
try {
|
|
322
|
-
remove(marker);
|
|
323
|
-
} catch (error) {
|
|
324
|
-
if (!failed) firstError = error;
|
|
325
|
-
failed = true;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
if (failed) {
|
|
329
|
-
throw firstError;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
4394
|
export {
|
|
4395
|
+
attachTileflowFairUseNotice,
|
|
333
4396
|
attachTileflowMapLifecycle,
|
|
334
|
-
createTileflowMarkerController,
|
|
335
4397
|
createTileflowSessionStarter,
|
|
336
|
-
|
|
4398
|
+
createTileflowThemeController,
|
|
4399
|
+
createTileflowTransformRequest,
|
|
4400
|
+
getTileflowSystemColorScheme,
|
|
4401
|
+
loadTileflowStyleFonts,
|
|
4402
|
+
registerTileflowContourProtocol,
|
|
4403
|
+
registerTileflowPmtilesProtocol,
|
|
4404
|
+
registerTileflowWorldRequestBridge,
|
|
4405
|
+
subscribeTileflowSystemColorScheme,
|
|
4406
|
+
tileflowFairUseNoticeMessage,
|
|
4407
|
+
tileflowMaplibreContourVersion,
|
|
4408
|
+
tileflowPmtilesMaximumDirectoryDepth,
|
|
4409
|
+
tileflowPmtilesProtocol
|
|
337
4410
|
};
|
|
4411
|
+
/*! Bundled license information:
|
|
4412
|
+
|
|
4413
|
+
maplibre-contour/dist/index.mjs:
|
|
4414
|
+
(*! *****************************************************************************
|
|
4415
|
+
Copyright (c) Microsoft Corporation.
|
|
4416
|
+
|
|
4417
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
4418
|
+
purpose with or without fee is hereby granted.
|
|
4419
|
+
|
|
4420
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
4421
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
4422
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
4423
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
4424
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
4425
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
4426
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
4427
|
+
***************************************************************************** *)
|
|
4428
|
+
*/
|