styled-map-package 4.0.1 → 5.0.0-pre.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/bin/smp-download.js +10 -6
  2. package/bin/smp-mbtiles.js +5 -7
  3. package/bin/smp-view.js +34 -26
  4. package/lib/reporters.js +92 -0
  5. package/package.json +8 -187
  6. package/LICENSE.md +0 -7
  7. package/README.md +0 -28
  8. package/dist/download.cjs +0 -101
  9. package/dist/download.d.cts +0 -65
  10. package/dist/download.d.ts +0 -65
  11. package/dist/download.js +0 -77
  12. package/dist/from-mbtiles.cjs +0 -91
  13. package/dist/from-mbtiles.d.cts +0 -17
  14. package/dist/from-mbtiles.d.ts +0 -17
  15. package/dist/from-mbtiles.js +0 -57
  16. package/dist/index.cjs +0 -49
  17. package/dist/index.d.cts +0 -27
  18. package/dist/index.d.ts +0 -27
  19. package/dist/index.js +0 -18
  20. package/dist/reader-watch.cjs +0 -135
  21. package/dist/reader-watch.d.cts +0 -24
  22. package/dist/reader-watch.d.ts +0 -24
  23. package/dist/reader-watch.js +0 -101
  24. package/dist/reader.cjs +0 -167
  25. package/dist/reader.d.cts +0 -62
  26. package/dist/reader.d.ts +0 -62
  27. package/dist/reader.js +0 -138
  28. package/dist/reporters.cjs +0 -122
  29. package/dist/reporters.d.cts +0 -10
  30. package/dist/reporters.d.ts +0 -10
  31. package/dist/reporters.js +0 -88
  32. package/dist/server.cjs +0 -78
  33. package/dist/server.d.cts +0 -48
  34. package/dist/server.d.ts +0 -48
  35. package/dist/server.js +0 -54
  36. package/dist/style-downloader.cjs +0 -312
  37. package/dist/style-downloader.d.cts +0 -120
  38. package/dist/style-downloader.d.ts +0 -120
  39. package/dist/style-downloader.js +0 -288
  40. package/dist/tile-downloader.cjs +0 -158
  41. package/dist/tile-downloader.d.cts +0 -84
  42. package/dist/tile-downloader.d.ts +0 -84
  43. package/dist/tile-downloader.js +0 -126
  44. package/dist/types-B4Xn1F9K.d.cts +0 -189
  45. package/dist/types-B4Xn1F9K.d.ts +0 -189
  46. package/dist/utils/errors.cjs +0 -41
  47. package/dist/utils/errors.d.cts +0 -18
  48. package/dist/utils/errors.d.ts +0 -18
  49. package/dist/utils/errors.js +0 -16
  50. package/dist/utils/fetch.cjs +0 -96
  51. package/dist/utils/fetch.d.cts +0 -51
  52. package/dist/utils/fetch.d.ts +0 -51
  53. package/dist/utils/fetch.js +0 -62
  54. package/dist/utils/file-formats.cjs +0 -98
  55. package/dist/utils/file-formats.d.cts +0 -35
  56. package/dist/utils/file-formats.d.ts +0 -35
  57. package/dist/utils/file-formats.js +0 -62
  58. package/dist/utils/geo.cjs +0 -84
  59. package/dist/utils/geo.d.cts +0 -46
  60. package/dist/utils/geo.d.ts +0 -46
  61. package/dist/utils/geo.js +0 -56
  62. package/dist/utils/mapbox.cjs +0 -121
  63. package/dist/utils/mapbox.d.cts +0 -43
  64. package/dist/utils/mapbox.d.ts +0 -43
  65. package/dist/utils/mapbox.js +0 -91
  66. package/dist/utils/misc.cjs +0 -39
  67. package/dist/utils/misc.d.cts +0 -22
  68. package/dist/utils/misc.d.ts +0 -22
  69. package/dist/utils/misc.js +0 -13
  70. package/dist/utils/streams.cjs +0 -130
  71. package/dist/utils/streams.d.cts +0 -73
  72. package/dist/utils/streams.d.ts +0 -73
  73. package/dist/utils/streams.js +0 -103
  74. package/dist/utils/style.cjs +0 -126
  75. package/dist/utils/style.d.cts +0 -69
  76. package/dist/utils/style.d.ts +0 -69
  77. package/dist/utils/style.js +0 -98
  78. package/dist/utils/templates.cjs +0 -114
  79. package/dist/utils/templates.d.cts +0 -78
  80. package/dist/utils/templates.d.ts +0 -78
  81. package/dist/utils/templates.js +0 -79
  82. package/dist/writer.cjs +0 -401
  83. package/dist/writer.d.cts +0 -7
  84. package/dist/writer.d.ts +0 -7
  85. package/dist/writer.js +0 -374
package/dist/writer.cjs DELETED
@@ -1,401 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var writer_exports = {};
30
- __export(writer_exports, {
31
- SUPPORTED_SOURCE_TYPES: () => SUPPORTED_SOURCE_TYPES,
32
- Writer: () => Writer
33
- });
34
- module.exports = __toCommonJS(writer_exports);
35
- var import_maplibre_gl_style_spec = require("@maplibre/maplibre-gl-style-spec");
36
- var import_bbox = require("@turf/bbox");
37
- var import_archiver = __toESM(require("archiver"), 1);
38
- var import_events = require("events");
39
- var import_filter_obj = require("filter-obj");
40
- var import_fs = __toESM(require("fs"), 1);
41
- var import_p_event = require("p-event");
42
- var import_readable_stream = require("readable-stream");
43
- var import_stream = require("stream");
44
- var import_file_formats = require('./utils/file-formats.cjs');
45
- var import_geo = require('./utils/geo.cjs');
46
- var import_misc = require('./utils/misc.cjs');
47
- var import_streams = require('./utils/streams.cjs');
48
- var import_style = require('./utils/style.cjs');
49
- var import_templates = require('./utils/templates.cjs');
50
- const SUPPORTED_SOURCE_TYPES = (
51
- /** @type {const} */
52
- [
53
- "raster",
54
- "vector",
55
- "geojson"
56
- ]
57
- );
58
- class Writer extends import_events.EventEmitter {
59
- #archive = (0, import_archiver.default)("zip", { zlib: { level: 9 } }).setMaxListeners(Infinity).on("error", console.error);
60
- /** @type {Set<string>} */
61
- #addedFiles = /* @__PURE__ */ new Set();
62
- /** @type {Set<string>} */
63
- #fonts = /* @__PURE__ */ new Set();
64
- /** @type {Set<string>} */
65
- #addedSpriteIds = /* @__PURE__ */ new Set();
66
- /** @type {Map<string, SourceInfo>} */
67
- #sources = /* @__PURE__ */ new Map();
68
- /** @type {StyleSpecification} */
69
- #style;
70
- #outputStream;
71
- static SUPPORTED_SOURCE_TYPES = SUPPORTED_SOURCE_TYPES;
72
- /**
73
- * @param {any} style A v7 or v8 MapLibre style. v7 styles will be migrated to
74
- * v8. (There are currently no typescript declarations for v7 styles, hence
75
- * this is typed as `any` and validated internally)
76
- * @param {object} opts
77
- * @param {number} [opts.highWaterMark=1048576] The maximum number of bytes to buffer during write
78
- */
79
- constructor(style, { highWaterMark = 1024 * 1024 } = {}) {
80
- super();
81
- if (!style || !("version" in style)) {
82
- throw new Error("Invalid style");
83
- }
84
- if (style.version !== 7 && style.version !== 8) {
85
- throw new Error(`Invalid style: Unsupported version v${style.version}`);
86
- }
87
- if (!Array.isArray(style.layers)) {
88
- throw new Error("Invalid style: missing layers property");
89
- }
90
- const styleCopy = (0, import_misc.clone)(style);
91
- (0, import_maplibre_gl_style_spec.migrate)(styleCopy);
92
- const errors = (0, import_maplibre_gl_style_spec.validateStyleMin)(styleCopy);
93
- if (errors.length) {
94
- throw new AggregateError(errors, "Invalid style");
95
- }
96
- this.#style = styleCopy;
97
- for (const [sourceId, source] of Object.entries(this.#style.sources)) {
98
- if (source.type !== "geojson") continue;
99
- this.#addSource(sourceId, source);
100
- }
101
- this.#outputStream = new import_readable_stream.PassThrough({ highWaterMark });
102
- (0, import_readable_stream.pipeline)(this.#archive, this.#outputStream, (err) => {
103
- if (err) this.emit("error", err);
104
- });
105
- }
106
- /**
107
- * @returns {import('stream').Readable} Readable stream of the styled map package
108
- */
109
- get outputStream() {
110
- return this.#outputStream;
111
- }
112
- #getBounds() {
113
- let bounds;
114
- let maxzoom = 0;
115
- for (const { source } of this.#sources.values()) {
116
- if (source.type === "geojson") {
117
- if (isEmptyFeatureCollection(source.data)) continue;
118
- const bbox2 = get2DBBox(source.data.bbox);
119
- bounds = bounds ? (0, import_geo.unionBBox)([bounds, bbox2]) : [...bbox2];
120
- } else {
121
- if (source.maxzoom < maxzoom) continue;
122
- if (source.maxzoom === maxzoom) {
123
- bounds = bounds ? (0, import_geo.unionBBox)([bounds, source.bounds]) : source.bounds;
124
- } else {
125
- bounds = source.bounds;
126
- maxzoom = source.maxzoom;
127
- }
128
- }
129
- }
130
- return bounds;
131
- }
132
- #getMaxZoom() {
133
- let maxzoom = 0;
134
- for (const { source } of this.#sources.values()) {
135
- const sourceMaxzoom = (
136
- // For GeoJSON sources, the maxzoom is 16 unless otherwise set
137
- source.type === "geojson" ? source.maxzoom || 16 : source.maxzoom
138
- );
139
- maxzoom = Math.max(maxzoom, sourceMaxzoom);
140
- }
141
- return maxzoom;
142
- }
143
- /**
144
- * Add a source definition to the styled map package
145
- *
146
- * @param {string} sourceId
147
- * @param {InputSource} source
148
- * @returns {SourceInfo}
149
- */
150
- #addSource(sourceId, source) {
151
- const encodedSourceId = encodeSourceId(this.#sources.size);
152
- const tileSourceOverrides = {
153
- minzoom: 0,
154
- maxzoom: 0,
155
- bounds: (
156
- /** @type {import('./utils/geo.js').BBox} */
157
- [...import_geo.MAX_BOUNDS]
158
- ),
159
- tiles: (
160
- /** @type {string[]} */
161
- []
162
- )
163
- };
164
- let smpSource;
165
- switch (source.type) {
166
- case "raster":
167
- case "vector":
168
- smpSource = {
169
- ...(0, import_filter_obj.excludeKeys)(source, ["tiles", "url"]),
170
- ...tileSourceOverrides
171
- };
172
- break;
173
- case "geojson":
174
- smpSource = {
175
- ...source,
176
- maxzoom: 0,
177
- data: typeof source.data !== "string" ? (
178
- // Add a bbox property to the GeoJSON data if it doesn't already have one
179
- { ...source.data, bbox: source.data.bbox || (0, import_bbox.bbox)(source.data) }
180
- ) : (
181
- // If GeoJSON data is referenced by a URL, start with an empty FeatureCollection
182
- {
183
- type: "FeatureCollection",
184
- features: [],
185
- bbox: [0, 0, 0, 0]
186
- }
187
- )
188
- };
189
- break;
190
- }
191
- const sourceInfo = {
192
- source: smpSource,
193
- encodedSourceId
194
- };
195
- this.#sources.set(sourceId, sourceInfo);
196
- return sourceInfo;
197
- }
198
- /**
199
- * Add a tile to the styled map package
200
- *
201
- * @param {Source} tileData
202
- * @param {TileInfo} opts
203
- */
204
- async addTile(tileData, { z, x, y, sourceId, format }) {
205
- let sourceInfo = this.#sources.get(sourceId);
206
- if (!sourceInfo) {
207
- const source2 = this.#style.sources[sourceId];
208
- if (!source2) {
209
- throw new Error(`Source not referenced in style.json: ${sourceId}`);
210
- }
211
- if (source2.type !== "raster" && source2.type !== "vector") {
212
- throw new Error(`Unsupported source type: ${source2.type}`);
213
- }
214
- sourceInfo = this.#addSource(sourceId, source2);
215
- }
216
- const { source, encodedSourceId } = sourceInfo;
217
- if (source.type !== "raster" && source.type !== "vector") {
218
- throw new Error(`Unsupported source type: ${source.type}`);
219
- }
220
- if (!format) {
221
- const tileDataStream = typeof tileData === "string" ? import_fs.default.createReadStream(tileData) : tileData instanceof Uint8Array ? import_stream.Readable.from(tileData) : tileData;
222
- [format, tileData] = await (0, import_file_formats.getTileFormatFromStream)(tileDataStream);
223
- }
224
- if (!sourceInfo.format) {
225
- sourceInfo.format = format;
226
- } else if (sourceInfo.format !== format) {
227
- throw new Error(
228
- `Tile format mismatch for source ${sourceId}: expected ${sourceInfo.format}, got ${format}`
229
- );
230
- }
231
- const bbox2 = (0, import_geo.tileToBBox)({ z, x, y });
232
- if (z > source.maxzoom) {
233
- source.maxzoom = z;
234
- source.bounds = bbox2;
235
- } else if (z === source.maxzoom) {
236
- source.bounds = (0, import_geo.unionBBox)([source.bounds, bbox2]);
237
- }
238
- const name = (0, import_templates.getTileFilename)({ sourceId: encodedSourceId, z, x, y, format });
239
- return this.#append(tileData, { name, store: true });
240
- }
241
- /**
242
- * Create a write stream for adding tiles to the styled map package
243
- *
244
- * @param {object} opts
245
- * @param {number} [opts.concurrency=16] The number of concurrent writes
246
- *
247
- * @returns
248
- */
249
- createTileWriteStream({ concurrency = 16 } = {}) {
250
- return (0, import_streams.writeStreamFromAsync)(this.addTile.bind(this), { concurrency });
251
- }
252
- /**
253
- * Add a sprite to the styled map package
254
- *
255
- * @param {object} options
256
- * @param {Source} options.json
257
- * @param {Source} options.png
258
- * @param {number} [options.pixelRatio]
259
- * @param {string} [options.id='default']
260
- * @returns {Promise<void>}
261
- */
262
- async addSprite({ json, png, pixelRatio = 1, id = "default" }) {
263
- this.#addedSpriteIds.add(id);
264
- const jsonName = (0, import_templates.getSpriteFilename)({ id, pixelRatio, ext: ".json" });
265
- const pngName = (0, import_templates.getSpriteFilename)({ id, pixelRatio, ext: ".png" });
266
- await Promise.all([
267
- this.#append(json, { name: jsonName }),
268
- this.#append(png, { name: pngName })
269
- ]);
270
- }
271
- /**
272
- * Add glyphs to the styled map package
273
- *
274
- * @param {Source} glyphData
275
- * @param {GlyphInfo} glyphInfo
276
- * @returns {Promise<void>}
277
- */
278
- addGlyphs(glyphData, { font: fontName, range }) {
279
- this.#fonts.add(fontName);
280
- const name = (0, import_templates.getGlyphFilename)({ fontstack: fontName, range });
281
- return this.#append(glyphData, { name });
282
- }
283
- /**
284
- * Create a write stream for adding glyphs to the styled map package
285
- *
286
- * @param {object} opts
287
- * @param {number} [opts.concurrency=16] The number of concurrent writes
288
- * @returns
289
- */
290
- createGlyphWriteStream({ concurrency = 16 } = {}) {
291
- return (0, import_streams.writeStreamFromAsync)(this.addGlyphs.bind(this), { concurrency });
292
- }
293
- /**
294
- * Finalize the styled map package and write the style to the archive.
295
- * This method must be called to complete the archive.
296
- * You must wait for your destination write stream to 'finish' before using the output.
297
- */
298
- finish() {
299
- this.#prepareStyle();
300
- const style = JSON.stringify(this.#style);
301
- this.#append(style, { name: import_templates.STYLE_FILE });
302
- this.#archive.finalize();
303
- }
304
- /**
305
- * Mutates the style object to prepare it for writing to the archive.
306
- * Deterministic: can be run more than once with the same result.
307
- */
308
- #prepareStyle() {
309
- if (this.#sources.size === 0) {
310
- throw new Error("Missing sources: add at least one source");
311
- }
312
- if (this.#style.glyphs && this.#fonts.size === 0) {
313
- throw new Error(
314
- "Missing fonts: style references glyphs but no fonts added"
315
- );
316
- }
317
- (0, import_style.replaceFontStacks)(this.#style, [...this.#fonts]);
318
- if (this.#style.glyphs) {
319
- this.#style.glyphs = import_templates.GLYPH_URI;
320
- }
321
- if (typeof this.#style.sprite === "string") {
322
- if (!this.#addedSpriteIds.has("default")) {
323
- throw new Error(
324
- "Missing sprite: style references sprite but none added"
325
- );
326
- }
327
- this.#style.sprite = (0, import_templates.getSpriteUri)();
328
- } else if (Array.isArray(this.#style.sprite)) {
329
- this.#style.sprite = this.#style.sprite.map(({ id }) => {
330
- if (!this.#addedSpriteIds.has(id)) {
331
- throw new Error(
332
- `Missing sprite: style references sprite ${id} but none added`
333
- );
334
- }
335
- return { id, url: (0, import_templates.getSpriteUri)(id) };
336
- });
337
- }
338
- this.#style.sources = {};
339
- for (const [sourceId, { source, encodedSourceId, format = "mvt" }] of this.#sources) {
340
- if (source.type === "geojson" && isEmptyFeatureCollection(source.data)) {
341
- continue;
342
- }
343
- this.#style.sources[sourceId] = source;
344
- if (!("tiles" in source)) continue;
345
- source.tiles = [(0, import_templates.getTileUri)({ sourceId: encodedSourceId, format })];
346
- }
347
- this.#style.layers = this.#style.layers.filter(
348
- (layer) => !("source" in layer) || !!this.#style.sources[layer.source]
349
- );
350
- const metadata = this.#style.metadata || (this.#style.metadata = {});
351
- const bounds = this.#getBounds();
352
- if (bounds) {
353
- metadata["smp:bounds"] = bounds;
354
- const [w, s, e, n] = bounds;
355
- this.#style.center = [w + (e - w) / 2, s + (n - s) / 2];
356
- }
357
- metadata["smp:maxzoom"] = this.#getMaxZoom();
358
- metadata["smp:sourceFolders"] = {};
359
- for (const [sourceId, { encodedSourceId }] of this.#sources) {
360
- metadata["smp:sourceFolders"][sourceId] = encodedSourceId;
361
- }
362
- this.#style.zoom = Math.max(0, this.#getMaxZoom() - 2);
363
- }
364
- /**
365
- *
366
- * @param {Source} source
367
- * @param {{ name: string, store?: boolean }} options
368
- * @returns {Promise<void>}
369
- */
370
- async #append(source, { name, store = false }) {
371
- if (this.#addedFiles.has(name)) {
372
- throw new Error(`${name} already added`);
373
- }
374
- this.#addedFiles.add(name);
375
- const onAdded = (0, import_p_event.pEvent)(
376
- this.#archive,
377
- "entry",
378
- (entry) => entry.name === name
379
- );
380
- this.#archive.append(convertSource(source), { name, store });
381
- await onAdded;
382
- }
383
- }
384
- function encodeSourceId(sourceIndex) {
385
- return sourceIndex.toString(36);
386
- }
387
- function convertSource(source) {
388
- return !Buffer.isBuffer(source) && source instanceof Uint8Array ? Buffer.from(source.buffer, source.byteOffset, source.length) : source;
389
- }
390
- function isEmptyFeatureCollection(data) {
391
- return data.type === "FeatureCollection" && data.features.length === 0;
392
- }
393
- function get2DBBox(bbox2) {
394
- if (bbox2.length === 4) return bbox2;
395
- return [bbox2[0], bbox2[1], bbox2[3], bbox2[4]];
396
- }
397
- // Annotate the CommonJS export names for ESM import in node:
398
- 0 && (module.exports = {
399
- SUPPORTED_SOURCE_TYPES,
400
- Writer
401
- });
package/dist/writer.d.cts DELETED
@@ -1,7 +0,0 @@
1
- export { G as GlyphInfo, d as GlyphRange, e as SUPPORTED_SOURCE_TYPES, f as Source, h as SourceInfo, g as SourceInternal, c as TileFormat, T as TileInfo, W as Writer } from './types-B4Xn1F9K.cjs';
2
- import 'readable-stream';
3
- import 'stream';
4
- import 'events';
5
- import '@maplibre/maplibre-gl-style-spec';
6
- import 'geojson';
7
- import 'type-fest';
package/dist/writer.d.ts DELETED
@@ -1,7 +0,0 @@
1
- export { G as GlyphInfo, d as GlyphRange, e as SUPPORTED_SOURCE_TYPES, f as Source, h as SourceInfo, g as SourceInternal, c as TileFormat, T as TileInfo, W as Writer } from './types-B4Xn1F9K.js';
2
- import 'readable-stream';
3
- import 'stream';
4
- import 'events';
5
- import '@maplibre/maplibre-gl-style-spec';
6
- import 'geojson';
7
- import 'type-fest';