styled-map-package 2.2.1 → 4.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 +15 -4
  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 +48 -0
  30. package/dist/server.d.ts +48 -0
  31. package/dist/server.js +55 -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 +107 -41
  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 __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var server_exports = {};
20
+ __export(server_exports, {
21
+ createServer: () => createServer
22
+ });
23
+ module.exports = __toCommonJS(server_exports);
24
+ var import_IttyRouter = require("itty-router/IttyRouter");
25
+ var import_StatusError = require("itty-router/StatusError");
26
+ var import_createResponse = require("itty-router/createResponse");
27
+ var import_node_stream = require("node:stream");
28
+ var import_errors = require('./utils/errors.cjs');
29
+ function resourceResponse(resource, options = {}) {
30
+ const response = new Response(
31
+ // @ts-expect-error Some discrepancy between Typescript lib dom typings and @types/node typings
32
+ import_node_stream.Readable.toWeb(resource.stream),
33
+ options
34
+ );
35
+ response.headers.set("Content-Type", resource.contentType);
36
+ response.headers.set("Content-Length", resource.contentLength.toString());
37
+ if (resource.contentEncoding) {
38
+ response.headers.set("Content-Encoding", resource.contentEncoding);
39
+ }
40
+ return response;
41
+ }
42
+ const jsonRaw = (0, import_createResponse.createResponse)("application/json; charset=utf-8");
43
+ const encoder = new TextEncoder();
44
+ function json(obj) {
45
+ const data = encoder.encode(JSON.stringify(obj));
46
+ return jsonRaw(data, {
47
+ headers: { "Content-Length": data.length.toString() }
48
+ });
49
+ }
50
+ function createServer({ base = "/" } = {}) {
51
+ base = base.endsWith("/") ? base : base + "/";
52
+ const router = (0, import_IttyRouter.IttyRouter)({
53
+ base
54
+ }).get("/style.json", async (request, reader) => {
55
+ const baseUrl = new URL(base, request.url);
56
+ const style = await reader.getStyle(baseUrl.href);
57
+ return json(style);
58
+ }).get("*", async (request, reader) => {
59
+ const url = new URL(request.url);
60
+ const path = decodeURIComponent(url.pathname.slice(base.length - 1));
61
+ const resource = await reader.getResource(path);
62
+ return resourceResponse(resource);
63
+ });
64
+ return {
65
+ fetch: (request, reader) => {
66
+ return router.fetch(request, reader).catch((err) => {
67
+ if ((0, import_errors.isFileNotThereError)(err)) {
68
+ throw new import_StatusError.StatusError(404, "Not Found");
69
+ } else {
70
+ throw err;
71
+ }
72
+ });
73
+ }
74
+ };
75
+ }
76
+ // Annotate the CommonJS export names for ESM import in node:
77
+ 0 && (module.exports = {
78
+ createServer
79
+ });
@@ -0,0 +1,48 @@
1
+ import { RequestLike } from 'itty-router';
2
+ import { Reader } from './reader.cjs';
3
+ import { IttyRouter } from 'itty-router/IttyRouter';
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
+ /**
14
+ * Create a server for serving styled map packages (SMP) over http. The server
15
+ * is a `fetch` handler that must be provided a WHATWG `Request` and a SMP
16
+ * `Reader` instance. Use `@whatwg-node/server` to use with Node.js HTTP server.
17
+ *
18
+ * To handle errors, catch errors from `fetch` and return appropriate HTTP responses.
19
+ * You can use `itty-router/error` for this.
20
+ *
21
+ * @example
22
+ * ```js
23
+ * import { createServer } from 'node:http'
24
+ * import { error } from 'itty-router/error'
25
+ * import { createServerAdapter } from '@whatwg-node/server'
26
+ * import { createServer as createSMPServer } from 'styled-map-package/server'
27
+ * import { Reader } from 'styled-map-package/reader'
28
+ *
29
+ * const reader = new Reader('path/to/your-style.smp')
30
+ * const smpServer = createSMPServer()
31
+ * const httpServer = createServer(createServerAdapter((request) => {
32
+ * return smpServer.fetch(request, reader).catch(error)
33
+ * }))
34
+ * ```
35
+ *
36
+ * @param {object} [options]
37
+ * @param {string} [options.base='/'] Base path for the server routes
38
+ * @returns {{ fetch: (request: RequestLike, reader: ReaderLike) => Promise<Response> }} server instance
39
+ */
40
+ declare function createServer({ base }?: {
41
+ base?: string | undefined;
42
+ }): {
43
+ fetch: (request: RequestLike, reader: ReaderLike) => Promise<Response>;
44
+ };
45
+ type ReaderLike = Pick<Reader, keyof Reader>;
46
+ type RouterType = typeof IttyRouter<IRequestStrict, [ReaderLike], Response>;
47
+
48
+ export { type ReaderLike, type RouterType, createServer };
@@ -0,0 +1,48 @@
1
+ import { RequestLike } from 'itty-router';
2
+ import { Reader } from './reader.js';
3
+ import { IttyRouter } from 'itty-router/IttyRouter';
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
+ /**
14
+ * Create a server for serving styled map packages (SMP) over http. The server
15
+ * is a `fetch` handler that must be provided a WHATWG `Request` and a SMP
16
+ * `Reader` instance. Use `@whatwg-node/server` to use with Node.js HTTP server.
17
+ *
18
+ * To handle errors, catch errors from `fetch` and return appropriate HTTP responses.
19
+ * You can use `itty-router/error` for this.
20
+ *
21
+ * @example
22
+ * ```js
23
+ * import { createServer } from 'node:http'
24
+ * import { error } from 'itty-router/error'
25
+ * import { createServerAdapter } from '@whatwg-node/server'
26
+ * import { createServer as createSMPServer } from 'styled-map-package/server'
27
+ * import { Reader } from 'styled-map-package/reader'
28
+ *
29
+ * const reader = new Reader('path/to/your-style.smp')
30
+ * const smpServer = createSMPServer()
31
+ * const httpServer = createServer(createServerAdapter((request) => {
32
+ * return smpServer.fetch(request, reader).catch(error)
33
+ * }))
34
+ * ```
35
+ *
36
+ * @param {object} [options]
37
+ * @param {string} [options.base='/'] Base path for the server routes
38
+ * @returns {{ fetch: (request: RequestLike, reader: ReaderLike) => Promise<Response> }} server instance
39
+ */
40
+ declare function createServer({ base }?: {
41
+ base?: string | undefined;
42
+ }): {
43
+ fetch: (request: RequestLike, reader: ReaderLike) => Promise<Response>;
44
+ };
45
+ type ReaderLike = Pick<Reader, keyof Reader>;
46
+ type RouterType = typeof IttyRouter<IRequestStrict, [ReaderLike], Response>;
47
+
48
+ export { type ReaderLike, type RouterType, createServer };
package/dist/server.js ADDED
@@ -0,0 +1,55 @@
1
+ import { IttyRouter } from "itty-router/IttyRouter";
2
+ import { StatusError } from "itty-router/StatusError";
3
+ import { createResponse } from "itty-router/createResponse";
4
+ import { Readable } from "node:stream";
5
+ import { isFileNotThereError } from "./utils/errors.js";
6
+ function resourceResponse(resource, options = {}) {
7
+ const response = new Response(
8
+ // @ts-expect-error Some discrepancy between Typescript lib dom typings and @types/node typings
9
+ Readable.toWeb(resource.stream),
10
+ options
11
+ );
12
+ response.headers.set("Content-Type", resource.contentType);
13
+ response.headers.set("Content-Length", resource.contentLength.toString());
14
+ if (resource.contentEncoding) {
15
+ response.headers.set("Content-Encoding", resource.contentEncoding);
16
+ }
17
+ return response;
18
+ }
19
+ const jsonRaw = createResponse("application/json; charset=utf-8");
20
+ const encoder = new TextEncoder();
21
+ function json(obj) {
22
+ const data = encoder.encode(JSON.stringify(obj));
23
+ return jsonRaw(data, {
24
+ headers: { "Content-Length": data.length.toString() }
25
+ });
26
+ }
27
+ function createServer({ base = "/" } = {}) {
28
+ base = base.endsWith("/") ? base : base + "/";
29
+ const router = IttyRouter({
30
+ base
31
+ }).get("/style.json", async (request, reader) => {
32
+ const baseUrl = new URL(base, request.url);
33
+ const style = await reader.getStyle(baseUrl.href);
34
+ return json(style);
35
+ }).get("*", async (request, reader) => {
36
+ const url = new URL(request.url);
37
+ const path = decodeURIComponent(url.pathname.slice(base.length - 1));
38
+ const resource = await reader.getResource(path);
39
+ return resourceResponse(resource);
40
+ });
41
+ return {
42
+ fetch: (request, reader) => {
43
+ return router.fetch(request, reader).catch((err) => {
44
+ if (isFileNotThereError(err)) {
45
+ throw new StatusError(404, "Not Found");
46
+ } else {
47
+ throw err;
48
+ }
49
+ });
50
+ }
51
+ };
52
+ }
53
+ export {
54
+ createServer
55
+ };