styled-map-package 2.2.0 → 3.0.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 (107) hide show
  1. package/bin/smp-download.js +3 -3
  2. package/bin/smp-mbtiles.js +1 -1
  3. package/bin/smp-view.js +2 -2
  4. package/dist/download.cjs +101 -0
  5. package/dist/download.d.cts +65 -0
  6. package/dist/{lib/download.d.ts → download.d.ts} +22 -6
  7. package/dist/download.js +77 -0
  8. package/dist/from-mbtiles.cjs +91 -0
  9. package/dist/from-mbtiles.d.cts +17 -0
  10. package/dist/from-mbtiles.d.ts +17 -0
  11. package/dist/from-mbtiles.js +57 -0
  12. package/dist/index.cjs +49 -0
  13. package/dist/index.d.cts +27 -0
  14. package/dist/index.d.ts +27 -0
  15. package/dist/index.js +18 -0
  16. package/dist/reader-watch.cjs +135 -0
  17. package/dist/reader-watch.d.cts +24 -0
  18. package/dist/reader-watch.d.ts +24 -0
  19. package/dist/reader-watch.js +101 -0
  20. package/dist/reader.cjs +167 -0
  21. package/dist/reader.d.cts +62 -0
  22. package/dist/{lib/reader.d.ts → reader.d.ts} +16 -5
  23. package/dist/reader.js +138 -0
  24. package/dist/reporters.cjs +122 -0
  25. package/dist/reporters.d.cts +10 -0
  26. package/dist/{lib/reporters.d.ts → reporters.d.ts} +5 -2
  27. package/dist/reporters.js +88 -0
  28. package/dist/server.cjs +79 -0
  29. package/dist/server.d.cts +37 -0
  30. package/dist/server.d.ts +37 -0
  31. package/dist/server.js +45 -0
  32. package/dist/style-downloader.cjs +312 -0
  33. package/dist/style-downloader.d.cts +120 -0
  34. package/dist/{lib/style-downloader.d.ts → style-downloader.d.ts} +23 -13
  35. package/dist/style-downloader.js +288 -0
  36. package/dist/tile-downloader.cjs +158 -0
  37. package/dist/tile-downloader.d.cts +84 -0
  38. package/dist/{lib/tile-downloader.d.ts → tile-downloader.d.ts} +22 -10
  39. package/dist/tile-downloader.js +126 -0
  40. package/dist/{lib/writer.d.ts → types-B4Xn1F9K.d.cts} +74 -14
  41. package/dist/types-B4Xn1F9K.d.ts +189 -0
  42. package/dist/utils/errors.cjs +41 -0
  43. package/dist/utils/errors.d.cts +18 -0
  44. package/dist/{lib/utils → utils}/errors.d.ts +4 -2
  45. package/dist/utils/errors.js +16 -0
  46. package/dist/utils/fetch.cjs +96 -0
  47. package/dist/utils/fetch.d.cts +51 -0
  48. package/dist/{lib/utils → utils}/fetch.d.ts +10 -4
  49. package/dist/utils/fetch.js +62 -0
  50. package/dist/utils/file-formats.cjs +98 -0
  51. package/dist/utils/file-formats.d.cts +35 -0
  52. package/dist/{lib/utils → utils}/file-formats.d.ts +13 -3
  53. package/dist/utils/file-formats.js +62 -0
  54. package/dist/utils/geo.cjs +84 -0
  55. package/dist/utils/geo.d.cts +46 -0
  56. package/dist/{lib/utils → utils}/geo.d.ts +8 -6
  57. package/dist/utils/geo.js +56 -0
  58. package/dist/utils/mapbox.cjs +121 -0
  59. package/dist/utils/mapbox.d.cts +43 -0
  60. package/dist/utils/mapbox.d.ts +43 -0
  61. package/dist/utils/mapbox.js +91 -0
  62. package/dist/utils/misc.cjs +39 -0
  63. package/{lib/utils/misc.js → dist/utils/misc.d.cts} +5 -9
  64. package/dist/{lib/utils → utils}/misc.d.ts +5 -3
  65. package/dist/utils/misc.js +13 -0
  66. package/dist/utils/streams.cjs +130 -0
  67. package/dist/utils/streams.d.cts +73 -0
  68. package/dist/{lib/utils → utils}/streams.d.ts +14 -10
  69. package/dist/utils/streams.js +103 -0
  70. package/dist/utils/style.cjs +126 -0
  71. package/dist/utils/style.d.cts +69 -0
  72. package/dist/{lib/utils → utils}/style.d.ts +19 -9
  73. package/dist/utils/style.js +98 -0
  74. package/dist/utils/templates.cjs +114 -0
  75. package/dist/utils/templates.d.cts +78 -0
  76. package/dist/{lib/utils → utils}/templates.d.ts +24 -14
  77. package/dist/utils/templates.js +79 -0
  78. package/dist/writer.cjs +401 -0
  79. package/dist/writer.d.cts +7 -0
  80. package/dist/writer.d.ts +7 -0
  81. package/dist/writer.js +374 -0
  82. package/package.json +87 -33
  83. package/dist/lib/from-mbtiles.d.ts +0 -13
  84. package/dist/lib/index.d.ts +0 -10
  85. package/dist/lib/reader-watch.d.ts +0 -13
  86. package/dist/lib/server.d.ts +0 -15
  87. package/dist/lib/types.d.ts +0 -64
  88. package/dist/lib/utils/mapbox.d.ts +0 -41
  89. package/lib/download.js +0 -114
  90. package/lib/from-mbtiles.js +0 -83
  91. package/lib/index.js +0 -11
  92. package/lib/reader-watch.js +0 -133
  93. package/lib/reader.js +0 -165
  94. package/lib/reporters.js +0 -92
  95. package/lib/server.js +0 -81
  96. package/lib/style-downloader.js +0 -363
  97. package/lib/tile-downloader.js +0 -188
  98. package/lib/types.ts +0 -104
  99. package/lib/utils/errors.js +0 -24
  100. package/lib/utils/fetch.js +0 -100
  101. package/lib/utils/file-formats.js +0 -85
  102. package/lib/utils/geo.js +0 -87
  103. package/lib/utils/mapbox.js +0 -155
  104. package/lib/utils/streams.js +0 -165
  105. package/lib/utils/style.js +0 -174
  106. package/lib/utils/templates.js +0 -136
  107. package/lib/writer.js +0 -478
package/dist/reader.js ADDED
@@ -0,0 +1,138 @@
1
+ import intoStream from "into-stream";
2
+ import { open } from "yauzl-promise";
3
+ import { json } from "node:stream/consumers";
4
+ import { ENOENT } from "./utils/errors.js";
5
+ import { noop } from "./utils/misc.js";
6
+ import { validateStyle } from "./utils/style.js";
7
+ import {
8
+ getContentType,
9
+ getResourceType,
10
+ STYLE_FILE,
11
+ URI_BASE
12
+ } from "./utils/templates.js";
13
+ class Reader {
14
+ /** @type {Promise<import('yauzl-promise').ZipFile>} */
15
+ #zipPromise;
16
+ #entriesPromise;
17
+ /** @type {undefined | Promise<void>} */
18
+ #closePromise;
19
+ /**
20
+ * @param {string | import('yauzl-promise').ZipFile} filepathOrZip Path to styled map package (`.styledmap`) file, or an instance of yauzl ZipFile
21
+ */
22
+ constructor(filepathOrZip) {
23
+ const zipPromise = this.#zipPromise = typeof filepathOrZip === "string" ? open(filepathOrZip) : Promise.resolve(filepathOrZip);
24
+ zipPromise.catch(noop);
25
+ this.#entriesPromise = (async () => {
26
+ const entries = /* @__PURE__ */ new Map();
27
+ if (this.#closePromise) return entries;
28
+ const zip = await zipPromise;
29
+ if (this.#closePromise) return entries;
30
+ for await (const entry of zip) {
31
+ if (this.#closePromise) return entries;
32
+ entries.set(entry.filename, entry);
33
+ }
34
+ return entries;
35
+ })();
36
+ this.#entriesPromise.catch(noop);
37
+ }
38
+ /**
39
+ * Resolves when the styled map package has been opened and the entries have
40
+ * been read. Throws any error that occurred during opening.
41
+ */
42
+ async opened() {
43
+ await this.#entriesPromise;
44
+ }
45
+ /**
46
+ * Get the style JSON from the styled map package. The URLs in the style JSON
47
+ * will be transformed to use the provided base URL.
48
+ *
49
+ * @param {string | null} [baseUrl] Base URL where you plan to serve the resources in this styled map package, e.g. `http://localhost:3000/maps/styleA`
50
+ * @returns {Promise<import('./types.js').SMPStyle>}
51
+ */
52
+ async getStyle(baseUrl = null) {
53
+ const styleEntry = (await this.#entriesPromise).get(STYLE_FILE);
54
+ if (!styleEntry) throw new ENOENT(STYLE_FILE);
55
+ const stream = await styleEntry.openReadStream();
56
+ const style = await json(stream);
57
+ if (!validateStyle(style)) {
58
+ throw new AggregateError(validateStyle.errors, "Invalid style");
59
+ }
60
+ if (typeof style.glyphs === "string") {
61
+ style.glyphs = getUrl(style.glyphs, baseUrl);
62
+ }
63
+ if (typeof style.sprite === "string") {
64
+ style.sprite = getUrl(style.sprite, baseUrl);
65
+ } else if (Array.isArray(style.sprite)) {
66
+ style.sprite = style.sprite.map(({ id, url }) => {
67
+ return { id, url: getUrl(url, baseUrl) };
68
+ });
69
+ }
70
+ for (const source of Object.values(style.sources)) {
71
+ if ("tiles" in source && source.tiles) {
72
+ source.tiles = source.tiles.map((tile) => getUrl(tile, baseUrl));
73
+ }
74
+ }
75
+ return (
76
+ /** @type {import('./types.js').SMPStyle} */
77
+ style
78
+ );
79
+ }
80
+ /**
81
+ * Get a resource from the styled map package. The path should be relative to
82
+ * the root of the package.
83
+ *
84
+ * @param {string} path
85
+ * @returns {Promise<Resource>}
86
+ */
87
+ async getResource(path) {
88
+ if (path[0] === "/") path = path.slice(1);
89
+ if (path === STYLE_FILE) {
90
+ const styleJSON = JSON.stringify(await this.getStyle());
91
+ return {
92
+ contentType: "application/json; charset=utf-8",
93
+ contentLength: Buffer.byteLength(styleJSON, "utf8"),
94
+ resourceType: "style",
95
+ stream: intoStream(styleJSON)
96
+ };
97
+ }
98
+ const entry = (await this.#entriesPromise).get(path);
99
+ if (!entry) throw new ENOENT(path);
100
+ const resourceType = getResourceType(path);
101
+ const contentType = getContentType(path);
102
+ const stream = await entry.openReadStream();
103
+ const resource = {
104
+ resourceType,
105
+ contentType,
106
+ contentLength: entry.uncompressedSize,
107
+ stream
108
+ };
109
+ if (path.endsWith(".gz")) {
110
+ resource.contentEncoding = "gzip";
111
+ }
112
+ return resource;
113
+ }
114
+ /**
115
+ * Close the styled map package file (should be called after reading the file to avoid memory leaks)
116
+ */
117
+ async close() {
118
+ if (this.#closePromise) return this.#closePromise;
119
+ this.#closePromise = (async () => {
120
+ const zip = await this.#zipPromise;
121
+ await zip.close();
122
+ })();
123
+ return this.#closePromise;
124
+ }
125
+ }
126
+ function getUrl(smpUri, baseUrl) {
127
+ if (!smpUri.startsWith(URI_BASE)) {
128
+ throw new Error(`Invalid SMP URI: ${smpUri}`);
129
+ }
130
+ if (typeof baseUrl !== "string") return smpUri;
131
+ if (!baseUrl.endsWith("/")) {
132
+ baseUrl += "/";
133
+ }
134
+ return smpUri.replace(URI_BASE, baseUrl);
135
+ }
136
+ export {
137
+ Reader
138
+ };
@@ -0,0 +1,122 @@
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 reporters_exports = {};
30
+ __export(reporters_exports, {
31
+ ttyReporter: () => ttyReporter
32
+ });
33
+ module.exports = __toCommonJS(reporters_exports);
34
+ var import_chalk = __toESM(require("chalk"), 1);
35
+ var import_log_symbols = __toESM(require("log-symbols"), 1);
36
+ var import_ora = __toESM(require("ora"), 1);
37
+ var import_pretty_bytes = __toESM(require("pretty-bytes"), 1);
38
+ var import_pretty_ms = __toESM(require("pretty-ms"), 1);
39
+ var import_readable_stream = require("readable-stream");
40
+ import_chalk.default.level = import_chalk.chalkStderr.level;
41
+ const TASKS = (
42
+ /** @type {const} */
43
+ [
44
+ "style",
45
+ "sprites",
46
+ "tiles",
47
+ "glyphs",
48
+ "output"
49
+ ]
50
+ );
51
+ const TASK_LABEL = (
52
+ /** @type {const} */
53
+ {
54
+ style: "Downloading Map Style",
55
+ sprites: "Downloading Sprites",
56
+ tiles: "Downloading Tiles",
57
+ glyphs: "Downloading Glyphs",
58
+ output: "Writing Styled Map Package"
59
+ }
60
+ );
61
+ const TASK_SUFFIX = (
62
+ /** @type {{ [K in (typeof TASKS)[number]]: (progress: import('./download.js').DownloadProgress[K]) => string }} */
63
+ {
64
+ style: () => "",
65
+ sprites: ({ downloaded }) => `${downloaded}`,
66
+ tiles: ({ total, skipped, totalBytes, downloaded }) => {
67
+ const formattedTotal = total.toLocaleString();
68
+ const formattedCompleted = (downloaded + skipped).toLocaleString().padStart(formattedTotal.length);
69
+ return `${formattedCompleted}/${formattedTotal} (${(0, import_pretty_bytes.default)(totalBytes)})`;
70
+ },
71
+ glyphs: ({ total, downloaded, totalBytes }) => `${downloaded}/${total} (${(0, import_pretty_bytes.default)(totalBytes)})`,
72
+ output: ({ totalBytes }) => `${(0, import_pretty_bytes.default)(totalBytes)}`
73
+ }
74
+ );
75
+ function ttyReporter() {
76
+ let stats;
77
+ let current = 0;
78
+ let spinner;
79
+ return new import_readable_stream.Writable({
80
+ objectMode: true,
81
+ // @ts-ignore - missing type def
82
+ construct(cb) {
83
+ process.stderr.write("\n");
84
+ spinner = (0, import_ora.default)(TASK_LABEL[TASKS[current]]).start();
85
+ cb();
86
+ },
87
+ /** @param {ArrayLike<{ chunk: import('./download.js').DownloadProgress, encoding: string }>} chunks */
88
+ writev(chunks, cb) {
89
+ stats = chunks[chunks.length - 1].chunk;
90
+ while (current < TASKS.length && stats[TASKS[current]].done) {
91
+ spinner.suffixText = import_chalk.default.dim(
92
+ TASK_SUFFIX[TASKS[current]](
93
+ // @ts-ignore - too complicated for TS
94
+ stats[TASKS[current]]
95
+ )
96
+ );
97
+ spinner.succeed();
98
+ if (++current < TASKS.length) {
99
+ spinner = (0, import_ora.default)(TASK_LABEL[TASKS[current]]).start();
100
+ }
101
+ }
102
+ if (current < TASKS.length) {
103
+ spinner.suffixText = import_chalk.default.dim(
104
+ TASK_SUFFIX[TASKS[current]](
105
+ // @ts-ignore - too complicated for TS
106
+ stats[TASKS[current]]
107
+ )
108
+ );
109
+ } else {
110
+ process.stderr.write(
111
+ `${import_chalk.default.green(import_log_symbols.default.success)} Completed in ${(0, import_pretty_ms.default)(stats.elapsedMs)}
112
+ `
113
+ );
114
+ }
115
+ cb();
116
+ }
117
+ });
118
+ }
119
+ // Annotate the CommonJS export names for ESM import in node:
120
+ 0 && (module.exports = {
121
+ ttyReporter
122
+ });
@@ -0,0 +1,10 @@
1
+ import { Writable } from 'readable-stream';
2
+
3
+ /**
4
+ * A writable stream to reporting download progress to a TTY terminal. Write
5
+ * progress messages to this stream for a pretty-printed progress task-list in
6
+ * the terminal.
7
+ */
8
+ declare function ttyReporter(): Writable;
9
+
10
+ export { ttyReporter };
@@ -1,7 +1,10 @@
1
+ import { Writable } from 'readable-stream';
2
+
1
3
  /**
2
4
  * A writable stream to reporting download progress to a TTY terminal. Write
3
5
  * progress messages to this stream for a pretty-printed progress task-list in
4
6
  * the terminal.
5
7
  */
6
- export function ttyReporter(): Writable;
7
- import { Writable } from 'readable-stream';
8
+ declare function ttyReporter(): Writable;
9
+
10
+ export { ttyReporter };
@@ -0,0 +1,88 @@
1
+ import chalk, { chalkStderr } from "chalk";
2
+ import logSymbols from "log-symbols";
3
+ import ora from "ora";
4
+ import prettyBytes from "pretty-bytes";
5
+ import prettyMilliseconds from "pretty-ms";
6
+ import { Writable } from "readable-stream";
7
+ chalk.level = chalkStderr.level;
8
+ const TASKS = (
9
+ /** @type {const} */
10
+ [
11
+ "style",
12
+ "sprites",
13
+ "tiles",
14
+ "glyphs",
15
+ "output"
16
+ ]
17
+ );
18
+ const TASK_LABEL = (
19
+ /** @type {const} */
20
+ {
21
+ style: "Downloading Map Style",
22
+ sprites: "Downloading Sprites",
23
+ tiles: "Downloading Tiles",
24
+ glyphs: "Downloading Glyphs",
25
+ output: "Writing Styled Map Package"
26
+ }
27
+ );
28
+ const TASK_SUFFIX = (
29
+ /** @type {{ [K in (typeof TASKS)[number]]: (progress: import('./download.js').DownloadProgress[K]) => string }} */
30
+ {
31
+ style: () => "",
32
+ sprites: ({ downloaded }) => `${downloaded}`,
33
+ tiles: ({ total, skipped, totalBytes, downloaded }) => {
34
+ const formattedTotal = total.toLocaleString();
35
+ const formattedCompleted = (downloaded + skipped).toLocaleString().padStart(formattedTotal.length);
36
+ return `${formattedCompleted}/${formattedTotal} (${prettyBytes(totalBytes)})`;
37
+ },
38
+ glyphs: ({ total, downloaded, totalBytes }) => `${downloaded}/${total} (${prettyBytes(totalBytes)})`,
39
+ output: ({ totalBytes }) => `${prettyBytes(totalBytes)}`
40
+ }
41
+ );
42
+ function ttyReporter() {
43
+ let stats;
44
+ let current = 0;
45
+ let spinner;
46
+ return new Writable({
47
+ objectMode: true,
48
+ // @ts-ignore - missing type def
49
+ construct(cb) {
50
+ process.stderr.write("\n");
51
+ spinner = ora(TASK_LABEL[TASKS[current]]).start();
52
+ cb();
53
+ },
54
+ /** @param {ArrayLike<{ chunk: import('./download.js').DownloadProgress, encoding: string }>} chunks */
55
+ writev(chunks, cb) {
56
+ stats = chunks[chunks.length - 1].chunk;
57
+ while (current < TASKS.length && stats[TASKS[current]].done) {
58
+ spinner.suffixText = chalk.dim(
59
+ TASK_SUFFIX[TASKS[current]](
60
+ // @ts-ignore - too complicated for TS
61
+ stats[TASKS[current]]
62
+ )
63
+ );
64
+ spinner.succeed();
65
+ if (++current < TASKS.length) {
66
+ spinner = ora(TASK_LABEL[TASKS[current]]).start();
67
+ }
68
+ }
69
+ if (current < TASKS.length) {
70
+ spinner.suffixText = chalk.dim(
71
+ TASK_SUFFIX[TASKS[current]](
72
+ // @ts-ignore - too complicated for TS
73
+ stats[TASKS[current]]
74
+ )
75
+ );
76
+ } else {
77
+ process.stderr.write(
78
+ `${chalk.green(logSymbols.success)} Completed in ${prettyMilliseconds(stats.elapsedMs)}
79
+ `
80
+ );
81
+ }
82
+ cb();
83
+ }
84
+ });
85
+ }
86
+ export {
87
+ ttyReporter
88
+ };
@@ -0,0 +1,79 @@
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 server_exports = {};
30
+ __export(server_exports, {
31
+ createServer: () => createServer
32
+ });
33
+ module.exports = __toCommonJS(server_exports);
34
+ var import_http_errors = __toESM(require("http-errors"), 1);
35
+ var import_reader = require('./reader.cjs');
36
+ var import_errors = require('./utils/errors.cjs');
37
+ var import_misc = require('./utils/misc.cjs');
38
+ function sendResource(reply, resource) {
39
+ reply.type(resource.contentType).header("content-length", resource.contentLength);
40
+ if (resource.contentEncoding) {
41
+ reply.header("content-encoding", resource.contentEncoding);
42
+ }
43
+ return reply.send(resource.stream);
44
+ }
45
+ function createServer(fastify, opts, done) {
46
+ const reader = "reader" in opts ? opts.reader : new import_reader.Reader(opts.filepath);
47
+ if (!("reader" in opts)) {
48
+ fastify.addHook("onClose", () => reader.close().catch(import_misc.noop));
49
+ }
50
+ fastify.get("/style.json", async () => {
51
+ try {
52
+ const baseUrl = new URL(fastify.prefix, fastify.listeningOrigin);
53
+ const style = await reader.getStyle(baseUrl.href);
54
+ return style;
55
+ } catch (error) {
56
+ if ((0, import_errors.isFileNotThereError)(error)) {
57
+ throw (0, import_http_errors.default)(404, error.message);
58
+ }
59
+ throw error;
60
+ }
61
+ });
62
+ fastify.get("*", async (request, reply) => {
63
+ const path = request.params["*"];
64
+ try {
65
+ const resource = await reader.getResource(path);
66
+ return sendResource(reply, resource);
67
+ } catch (error) {
68
+ if ((0, import_errors.isFileNotThereError)(error)) {
69
+ throw (0, import_http_errors.default)(404, error.message);
70
+ }
71
+ throw error;
72
+ }
73
+ });
74
+ done();
75
+ }
76
+ // Annotate the CommonJS export names for ESM import in node:
77
+ 0 && (module.exports = {
78
+ createServer
79
+ });
@@ -0,0 +1,37 @@
1
+ import * as http from 'http';
2
+ import * as fastify from 'fastify';
3
+ import { Reader } from './reader.cjs';
4
+ import 'stream';
5
+ import './types-B4Xn1F9K.cjs';
6
+ import '@maplibre/maplibre-gl-style-spec';
7
+ import 'geojson';
8
+ import 'type-fest';
9
+ import 'readable-stream';
10
+ import 'events';
11
+ import 'yauzl-promise';
12
+
13
+ declare function createServer(instance: fastify.FastifyInstance<fastify.RawServerDefault, http.IncomingMessage, http.ServerResponse<http.IncomingMessage>, fastify.FastifyBaseLogger, fastify.FastifyTypeProviderDefault>, opts: PluginOptions, done: (err
14
+ /**
15
+ * Fastify plugin for serving a styled map package.
16
+ *
17
+ * If you provide a `Reader` (or `ReaderWatch`) instance via the `reader` opt,
18
+ * you must manually close the instance yourself.
19
+ *
20
+ * @type {FastifyPluginCallback<PluginOptions>}
21
+ */
22
+ ?: Error) => void): void;
23
+ type PluginOptionsFilepath = {
24
+ /**
25
+ * Path to styled map package (`.smp`) file
26
+ */
27
+ filepath: string;
28
+ };
29
+ type PluginOptionsReader = {
30
+ /**
31
+ * SMP Reader interface (also supports ReaderWatch)
32
+ */
33
+ reader: Pick<Reader, keyof Reader>;
34
+ };
35
+ type PluginOptions = PluginOptionsFilepath | PluginOptionsReader;
36
+
37
+ export { type PluginOptions, type PluginOptionsFilepath, type PluginOptionsReader, createServer };
@@ -0,0 +1,37 @@
1
+ import * as http from 'http';
2
+ import * as fastify from 'fastify';
3
+ import { Reader } from './reader.js';
4
+ import 'stream';
5
+ import './types-B4Xn1F9K.js';
6
+ import '@maplibre/maplibre-gl-style-spec';
7
+ import 'geojson';
8
+ import 'type-fest';
9
+ import 'readable-stream';
10
+ import 'events';
11
+ import 'yauzl-promise';
12
+
13
+ declare function createServer(instance: fastify.FastifyInstance<fastify.RawServerDefault, http.IncomingMessage, http.ServerResponse<http.IncomingMessage>, fastify.FastifyBaseLogger, fastify.FastifyTypeProviderDefault>, opts: PluginOptions, done: (err
14
+ /**
15
+ * Fastify plugin for serving a styled map package.
16
+ *
17
+ * If you provide a `Reader` (or `ReaderWatch`) instance via the `reader` opt,
18
+ * you must manually close the instance yourself.
19
+ *
20
+ * @type {FastifyPluginCallback<PluginOptions>}
21
+ */
22
+ ?: Error) => void): void;
23
+ type PluginOptionsFilepath = {
24
+ /**
25
+ * Path to styled map package (`.smp`) file
26
+ */
27
+ filepath: string;
28
+ };
29
+ type PluginOptionsReader = {
30
+ /**
31
+ * SMP Reader interface (also supports ReaderWatch)
32
+ */
33
+ reader: Pick<Reader, keyof Reader>;
34
+ };
35
+ type PluginOptions = PluginOptionsFilepath | PluginOptionsReader;
36
+
37
+ export { type PluginOptions, type PluginOptionsFilepath, type PluginOptionsReader, createServer };
package/dist/server.js ADDED
@@ -0,0 +1,45 @@
1
+ import createError from "http-errors";
2
+ import { Reader } from "./reader.js";
3
+ import { isFileNotThereError } from "./utils/errors.js";
4
+ import { noop } from "./utils/misc.js";
5
+ function sendResource(reply, resource) {
6
+ reply.type(resource.contentType).header("content-length", resource.contentLength);
7
+ if (resource.contentEncoding) {
8
+ reply.header("content-encoding", resource.contentEncoding);
9
+ }
10
+ return reply.send(resource.stream);
11
+ }
12
+ function createServer(fastify, opts, done) {
13
+ const reader = "reader" in opts ? opts.reader : new Reader(opts.filepath);
14
+ if (!("reader" in opts)) {
15
+ fastify.addHook("onClose", () => reader.close().catch(noop));
16
+ }
17
+ fastify.get("/style.json", async () => {
18
+ try {
19
+ const baseUrl = new URL(fastify.prefix, fastify.listeningOrigin);
20
+ const style = await reader.getStyle(baseUrl.href);
21
+ return style;
22
+ } catch (error) {
23
+ if (isFileNotThereError(error)) {
24
+ throw createError(404, error.message);
25
+ }
26
+ throw error;
27
+ }
28
+ });
29
+ fastify.get("*", async (request, reply) => {
30
+ const path = request.params["*"];
31
+ try {
32
+ const resource = await reader.getResource(path);
33
+ return sendResource(reply, resource);
34
+ } catch (error) {
35
+ if (isFileNotThereError(error)) {
36
+ throw createError(404, error.message);
37
+ }
38
+ throw error;
39
+ }
40
+ });
41
+ done();
42
+ }
43
+ export {
44
+ createServer
45
+ };